Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 266
maths Avatar asked Apr 12 '14 00:04

maths


People also ask

Can I run both Apache and Nginx?

Apache and Nginx can definitely run simultaneously. The default config will not allow them to start at the same time because they will both try to listen on the same port and the same IP.

When you configure Nginx as a reverse proxy for Apache both may listen to the same port?

You do that by configuring NGINX as a reverse proxy for Apache. With this setup, NGINX will listen for all incoming requests to port 80 and pass them on to Apache, which is listening in on port 8080.

Which is better Nginx or Apache?

In terms of performance, NGINX is much better than Apache. NGINX performs 2.5 times faster than Apache — and consumes less memory as well. However, Apache has more functionality and features. It is worth noting that it is possible to use both together.


3 Answers

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

like image 170
redmoon7777 Avatar answered Sep 23 '22 21:09

redmoon7777


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

like image 45
hassan ketabi Avatar answered Sep 19 '22 21:09

hassan ketabi


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

like image 38
Jekis Avatar answered Sep 19 '22 21:09

Jekis