Contact Us
How To Install Nginx with /var/www/html in CentOS 5/6/7 and Ubuntu Server
Before Getting Start, Know About Nginx
Nginx is the world second most in use web servers/application in the world. It is used for most of the websites which have higher web traffic in the world on the internet. Nginx is very much resource friendly web server application. Also, sometimes Nginx is used as a web server or a reverse proxy. Let’s install Nginx
Let’s Start
In this tutorial, we’ll discuss how to get Nginx installed on your centOS 5/6/7 Ubuntu Server within /var/www/html.
Scenario:
My Server IP: 192.168.1.188
OS: CentOS 5/6/7 and Ubuntu
Note: If you are a SUDO user then prefix every command with sudo, like #sudo ifconfig
Step 1: Install EPEL Repository
Installing EPEL will make sure that Nginx on CentOS stays up to date. Install EPEL, open terminal and type following command
For CentOS 5/6/7:
#sudo yum install epel-release
Note: We don’t require any action for Ubuntu as Ubuntu team provides Nginx package in its default repositories
Step 2: Install Nginx
To install Nginx on Centos, open terminal and type in:
For CentOS:
#sudo yum update #sudo yum install nginx
After you answer yes to the prompt twice (the first time relates to importing the EPEL gpg-key), Nginx will finish installing on your server.
For Ubuntu:
#sudo apt-get update #sudo apt-get install nginx #chkconfig --level 235 httpd off
Step 3: Nginx Default Configuration Changes
Now We Will Configure Nginx configuration file to map or Nginx server with /var/www/html. To Do this copy and paste under given configuration into the file /etc/nginx/nginx.conf
#>/etc/nginx/nginx.conf
Edit file using vim Editor
#vim /etc/nginx/nginx.conf
Copy paste this configuration in this file and save it.
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; index index.html index.htm; server { listen 80 default_server; listen [::]:80 default_server; server_name localhost; root /var/www/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root /var/www/html; # try_files $uri =404; #fastcgi_pass 127.0.0.1:9000; #fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; # } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # # location ~ /\.ht { # deny all; # } } }
After writing content in the file, save and quit the file using :wq!
Step 4: Remove Default Configurations
Now remove Default file because we have made our single configuration file
#mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf-bak
Step 5: Create File in /var/www/html Directory
Now All we have to do create our HTML directory and a file with ‘.html’ extension with the help of following commands.
#mkdir /var/www/html/;chmod -R 755 /var/www/html/ #touch /var/www/html/index.hmtl #chmod -R 777 /var/www/html/index.html #vim /var/www/html/index.html Hello I am in /var/www/html dir
After writing content in the file, save and quit the file using :wq!
Step 6: Start Nginx
Nginx does not start on its own. To get Nginx running, type:
On Centos
#/etc/init.d/nginx start
On Ubuntu
Service will start Automatically.
Now, you can confirm that Nginx has installed on your Server by directing your browser to your IP address. You can run the following command to reveal your server’s IP address.
For CentOs
#ifconfig eth0 | grep inet | awk '{ print $2 }'
For Ubuntu
#ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Step 7: Check Nginx
http://192.168.1.110 (Put Your Server’s Ip Address)
On the page, you will see the words, “Hello I am in /var/www/html dir”