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

I want to configure both Apache and nginx to run together on Ubuntu because I want to develop on both nginx and Apache. I have read that I have to edit the configuration on Apache or nginx to make one of them run on another port rather than 80.

Which files should I edit in Nginx to make it run through another port?


Solution 1:

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

Solution 2:

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

Solution 3:

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

Solution 4:

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

`

Solution 5:

if apache is running on port 80 then either you have to change apache listening port or nginx listening port.

to change nginx listening port:

open /etc/nginx/sites-available/default file with nano or any file editor of your choice and then change these 2 lines

 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;

to

 listen 8000 default_server;
 listen [::]:8000 default_server ipv6only=on;

if port 8000 is not bound to any other service.

and then reload nginx using service nginx reload