Saturday, May 13, 2017

How to run Nginx alongside Apache

How to run Nginx along side Apache web server

Technically you can run both nginx and apache web server on your lubuntu, but they can't be on the same port number. This article i'm going to show you how to run both web server on lubuntu 16.04. 

Nginx and apache web server not only use the same port number (port 80) as default port, but also share the same document root which is /var/www/html/. So basically in order to make them both running, we need to change the port number and document root.

The changes can be made for apache or nginx, For this tutorial i'm going to keep apache as it is and change the port number and document root for nginx. So basically here's what i'm going to do:
  • apache still using port 80
  • apache document root still in /var/www/html/
  • nginx will be using port 8181
  • nginx document root will be in /var/www/nginx/
So no change will be made for apache web server, but we are going to change nginx web server. Note that this tutorial assume you already install both apache web server and nginx web server.


Step by step how to change port number and document root Nginx 
  • open command line on lubuntu (press CTRL + ALT + T)
  • go to /var/www/ directory
  • cd /var/www/
  • create new directory called 'nginx'
  • sudo mkdir nginx
  • copy index.nginx-debian.html from /var/www/html/ to /var/www/nginx/
  • sudo cp /var/www/html/index.nginx-debian.html /var/www/nginx/
  • go to /etc/nginx/sites-enabled directory
  • cd /etc/nginx/sites-enabled/
  • open the config file using text editor (leafpad for example)
  • sudo leafpad default
  • find this line code
  • # Default server configuration
    #
    server {
     listen 80 default_server;
     listen [::]:80 default_server;
            ...
     root /var/www/html;
    
  • change it into like this
  • # Default server configuration
    #
    server {
     listen 8181 default_server;
     listen [::]:8181 default_server;
            ...
     root /var/www/nginx;
    
  • save and exit
  • restart nginx 
  • sudo service nginx restart

How to run Nginx along side Apache web server

You can open apache web server as usual on http://localhost/ and nginx on http://localhost:8181/.

No comments:

Post a Comment