Install Nginx on CentOS 7
Nginx is free, high performance and opensource web server available today. Nginx is used as the standalone web server or as the reverse proxy server for Apache (or other web servers). This tutorial outlines to install Nginx on CentOS 7.
Before you start installing Nginx on CentOS 7. You must have the non-root user account on your server with sudo privileges.
1. Install Nginx
First, you will need to update system software packages to the latest version. Then you can install Nginx.
Update system software packages by typing
sudo yum -y update
Nginx packages for CentOS are provided through EPEL repository. Enter the following command to install EPEL repository.
sudo yum install epel-release
Now install Nginx by typing
sudo yum install nginx
If you are installing packages through EPEL repository first time then you may be prompted to import EPEL GPG key. You should type ==y== and hit ==Enter== to continue.
After completing installation Enable and Start Nginx server by typing following command
sudo systemctl enable nginx sudo systemctl start nginx
Check the status of Nginx server by typing
sudo systemctl status nginx
The output should be:
● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2018-10-24 18:45:48 UTC; 2s ago Process: 1677 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1673 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1680 (nginx) CGroup: /system.slice/nginx.service ├─1680 nginx: master process /usr/sbin/nginx └─1681 nginx: worker process
2. Managing Firewall
If your server is protected by firewall then you will need to open ==HTTP== and ==HTTPS== ports
Open HTTP port by typing
sudo firewall-cmd --permanent --zone=public --add-service=http
Open HTTPS port by typing
sudo firewall-cmd --permanent --zone=public --add-service=https
Now reload firewall configuration files by typing
sudo firewall-cmd --reload
Now verify installation of Nginx by visiting the following URL
3. Managing Nginx Web Server Operations
You can manage Nginx Web Server Operations by using following commands.
If you have made any changes to the configuration file and want to reload configuration file then use the following command
sudo systemctl reload nginx
To start Nginx server enter following command
sudo systemctl start nginx
To stop Nginx server enter the following command
sudo systemctl stop nginx
Restart Nginx server you can use the following command
sudo systemctl restart nginx
Check the status of the Nginx server by typing
sudo systemctl status nginx
If you want to disable Nginx to auto start after boot then type following.
sudo systemctl disable nginx
And to enable auto starting service after boot use following command
sudo systemctl enable nginx
4. Nginx Important Files and Directories
* Root directory for Nginx files is ==etc/nginx==.
* Nginx main configuration file is available at ==etc/nginx/nginx.conf==.
* Virtual hosts(Server Blocks) Configuration files should be added in == etc/nginx/conf.d.
* The default server document root directory for web files is located at ==/usr/share/nginx/html==.
* Nginx log files both ==access.log== and ==error.log== are located inside ==/var/log/nginx/== directory.
You have learned successfully how to install Nginx on CentOS 7. If you have any queries please don’t forget to comment below.