I installed Nginx, but I have Apache too. How I can run Nginx? When I go to my IP I am getting /var/www/ directory. I already try to figure out solving in internet but still can't get what I am doing wrong.

Can somebody help me?

2 Answers 2


There are several ways to run Apache side by side with nginx on the same machine.

Here's one way: assuming you have both Apache and nginx installed on Ubuntu/Debian, follow these instructions.

The end result will be Apache as a "master" server and nginx as an Apache virtualhost.

Change the port for nginx

Open the nginx default site:

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

Change the port on the listen lines under server from 80 to 81:

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

Save and exit nano.

Enable the required modules for Apache

sudo a2enmod proxy_http

Set up the virtual host

cd /etc/apache2/sites-available
sudo nano nginx.conf

Paste this in to nano:

<VirtualHost *:80>

    # (optional) Set a ServerName:
    # ServerName nginx.mydomain.com

    # Set the below line to your desired subdomain:
    ServerAlias nginx.*

    <Directory />
        Options FollowSymLinks
        AllowOverride None

    ProxyRequests Off
    <Proxy *>
            Order deny,allow
            Allow from all

    # Forward requests to nginx (port 81)
    ProxyPass /
    ProxyPassReverse /


Note the ServerName and ServerAlias in the file above. Change these to your desired domain if you have one.

Save and exit nano.

If you don't have a domain and/or would like to run it locally only, open /etc/hosts:

sudo nano /etc/hosts

Then add these two lines to the bottom (you may change nginx.localhost to what you desire):

# nginx       nginx.localhost

Now enable nginx on Apache:

sudo a2ensite nginx
sudo service apache2 restart

View in your browser

http://nginx.localhost (or your desired domain that you configured)

A couple of notes


If you want to run nginx and not apache2 you have two option either uninstall apache2 or stop apache2

To uninstall Apache2

sudo apt-get purge apache2*
sudo apt-get autoremove
sudo apt-get autoclean
rm -rfv apache2*

or you can simply stop apache2 service

sudo service apache2 stop OR sudo service apache2ctl stop

default nginx root folder is /usr/share/nginx/html or /usr/share/nginx/www

you should also start nginx just for surety

sudo service nginx start

netstat -ntlp will show you which port is being used by which service 
  • 1
    You should add a path, where to start rm -rfv apache2*
    – A.B.
    Oct 19, 2015 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.