Setup Nginx Server

Install and setup Nginx server

1. Download Nginx

Download Nginx from here

1
2
3
wget http://nginx.org/download/nginx-1.11.12.tar.gz
tar xvfz nginx-1.11.12.tar.gz
cd nginx-1.11.12

2. Install Nginx

Install use Nginx configure file.

1
2
3
4
5
./configure

make

make install

show configure help

1
./configure --help

  • nginx will be installed under /usr/local/nginx as shown by the ./configure output.

3. Configure Nginx

Nginx is configured to listen by default on port 80.
Similar to apache’s httpd.conf file, nginx has nginx.conf file located under /usr/local/nginx/conf.
You can change port by change the nginx.conf file, change the port 80 to 8081.

1
2
3
4
5
vi /usr/local/nginx/conf/nginx.conf

server {
listen 8081;
server_name localhost;

4. Start and Stop Nginx Server

Nginx executable is located under /usr/local/nginx/sbin directory.

  • Just call this executable to start the nginx server.
1
2
3
cd /usr/local/nginx/sbin

./nginx
  • To stop the nginx server, do the following.
1
2
3
cd /usr/local/nginx/sbin

./nginx -s stop
  • To view the current version of nginx, do the following:
1
2
3
./nginx -v

nginx: nginx version: nginx/1.0.5
  • To debug issues, view the error.log and access.log files located under /usr/local/nginx/logs
1
2
3
4
5
ls /usr/local/nginx/logs/

access.log
error.log
nginx.pid

nginx tomcat reverse proxy

add this to nginx.conf file.

1
2
3
4
5
6
7
8
9
10
# /usr/local/nginx/conf/nginx.conf

listen 80;

location /tomcat {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/;
}

Change Apache default port

In /etc/apache2/ports.conf, change the port as

1
Listen 8079

Then go to /etc/apache2/sites-enabled/000-default.conf

And change the first line as

1
<VirtualHost *: 8079>

Now restart
1
sudo service apache2 restart