How to Install Nginx on CentOS, Debian & Ubuntu?
The Nginx is a free, open source, lightweight and high-performance web server designed for high-traffic use cases. It is also acts as a popular Web server behind the Apache Web server and Microsoft’s IIS. Nginx is more resource-friendly than Apache. It can be served in front of other web servers as a reverse proxy.
All Nginx configuration files are stored in the /etc/nginx/ directory and /etc/nginx/nginx.conf is the the primary configuration file.
The the default server root directory in centos is /usr/share/nginx/html and in ubuntu and debian it is /var/www/html
Also any additional server block (virtual hosts in apache 2) for a website can be added in the location /etc/nginx/conf.d
Installation on Centos7
Run the following commands to install Nginx:
yum install epel-release
yum install nginx
Nginx does not start automatically even after the installation is completed. Run the following command to start the Nginx process:
systemctl start nginx
Run the following command to make sure the service is running:
systemctl start nginx
output:
nginx (pid 2735) is running…
Run the following command to restart Nginx:
systemctl restart nginx
output:
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
Installation on Centos6
Run the following commands to install Nginx:
yum install epel-release
yum install nginx
Run the following command to start the Nginx process:
service nginx start
Run the following command to make sure the service is running:
service nginx status
output:
nginx (pid 2735) is running…
Run the following command to restart Nginx:
service nginx restart
output:
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
Installation on Ubuntu, Debian
Run the following command to install Nginx:
apt update
apt install nginx
Run the following command to start apache:
/etc/init.d/nginx start
Run the following command to make sure the service is running:
/etc/init.d/nginx status
Output:
nginx.service – A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2019-09-26 16:08:19 UTC; 1 day ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker proces
Run the following command to restart apache:
/etc/init.d/nginx restart
output:
[ ok ] Restarting nginx (via systemctl): nginx.service.
After installation is completed, you can check whether the Nginx is installed or not by entering server IP address into your browser’s address bar:
http://your_server_ip
You can see the default Nginx web page as shown below if it is successfully installed.