How to Install Nginx on CentOS, Debian & Ubuntu?
In this guide, we will show you how to install Nginx on CentOS, Debian, and Ubuntu. Whether you are setting up a web server or a reverse proxy, these simple steps will help you get Nginx running smoothly on your system. 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
Why Choose Nginx?
Nginx is a free, open-source software known for its high performance, stability, and low resource usage. It can act as both a web server and a reverse proxy, making it versatile for a variety of use cases, such as hosting websites, serving static content, and handling high-traffic loads.
- Performance: Nginx uses an asynchronous, event-driven model to handle multiple connections simultaneously, making it more efficient than traditional web servers like Apache.
- Low Resource Usage: Nginx can handle thousands of simultaneous connections with relatively low memory usage, making it ideal for high-traffic websites.
- Reverse Proxy: Nginx is also widely used as a reverse proxy server to balance the load between backend servers.
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.
By following these steps, you can easily install Nginx on CentOS, Debian, and Ubuntu systems. Nginx provides a robust, scalable, and lightweight solution for serving websites and acting as a reverse proxy. With proper configuration, Nginx can handle high-traffic websites efficiently and securely.