How can I run both nginx and Apache together on Ubuntu?

so i had a similar problem i had Apache running on Ubuntu 14.04 and i had to install nginx alongside the Apache so after installing nginx i changed the listening port of the ngnix server from default 80 to 81 and it did the magic follow the below instruction to achieve it

cd /etc/nginx/sites-available
sudo cp default default.bak
sudo nano default

change port here .

server {
listen 81 default_server;
listen [::]:81 default_server ipv6only=on;

save it and get out of nano then restart the ngnix with

sudo service nginx restart

and in browser http://localhost:81 your server is up

`


go to /etc/nginx/sites-available then modify the host file which should listen to a different port (if you didn't change anything here you will find a default file, enter to change it)

in the file change listen: 80 to the port you want to listen to

don't forget to reload the service: service nginx reload


Here is the answer how to have both Apache and NGINX installed on the same 80 port (on localhost).

Assuming that you have both NGINX and Apache installed...

1. Select different IP addresses for each one.

Let's setup the hosts file for quick access to start pages.

sudo nano /etc/hosts

append lines (use any local IP you like)

127.0.0.1   nginx
127.0.0.2   apache

2. Setup listen IP and port for NGINX

NGINX must listen on one IP address only.

sudo nano /etc/nginx/sites-enabled/default

And replace the lines

--- (remove lines) +++ (add lines)

--- listen 80 default_server;
--- listen [::]:80 default_server;
+++ listen nginx:80;

If you want to use SSL, make the same things for 443 port.

IMPORTANT!

Make sure all enabled NGINX websites listen on nginx:80

Restart NGINX

sudo service nginx restart

Make a check using command sudo netstat -tulpn | grep :80

tcp        0      0 127.0.0.1:80            0.0.0.0:*               LISTEN      26540/nginx: master

Done! Now you can access default NGINX host by url http://nginx

3. Setup listen IP and port for Apache

Apache must listen on one IP address only as well.

Ports:

sudo nano /etc/apache2/ports.conf

And replace the lines

--- (remove lines) +++ (add lines)

--- Listen 80
--- Listen 443
+++ Listen apache:80
+++ Listen apache:443

Default virtual host:

sudo nano /etc/apache/sites-enabled/000-default

And replace the lines

--- (remove lines) +++ (add lines)

--- <VirtualHost *:80>
+++ <VirtualHost apache:80>

If you want to use SSL, make the same things for 443 port.

IMPORTANT!

Make sure all enabled Apache websites listen on apache:80

Restart Apache

sudo service apache2 restart

Make a check using command sudo netstat -tulpn | grep :80

tcp        0      0 127.0.0.2:80            0.0.0.0:*               LISTEN      26829/apache2

Done! Now you can access default Apache host by url http://apache


It's better to make apache listen on a different port and instruct nginx to reverse-proxy dynamic traffic to your apache while serving static files by nginx.

For apache in /etc/apache2/ports.conf include:

Listen 8080

For further information refer: https://serverfault.com/questions/92943/server-has-apache-installed-how-to-install-nginx-alongside-it