Bootstrap FreeKB - Nginx (Web Server) - Install nginx on Linux
Nginx (Web Server) - Install nginx on Linux

Updated:   |  Nginx (Web Server) articles

If using a Debian distribution (Ubuntu, Mint), apt-get can be used to install Nginx. If using a Red Hat distribution (CentOS, Fedora, Red Hat), dnf or yum can be used to install Nginx.

yum install epel-release
yum install nginx

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to start and enable nginx.

systemctl enable nginx
systemctl start nginx
systemctl status nginx

 

If your system is using init, use the chkconfig and service commands to start and enable nginx.

chkconfig nginx on
service nginx start
service nginx status

 

Use the ps command to ensure no other processes on the server are using port 80.

ps -ef | grep 80

 

If nginx fails to start, check the error log.

cat /var/log/nginx/error.log

 

If you have a firewall, such as iptables or firewalld, allow HTTP on port 80 and HTTPS on port 443 in the firewall.

firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

 

Use the sestatus command to determine if SELinux is enforcing, permissive, or disabled.

~]# sestatus
Current mode:  enforcing

 

If SELinux is enforcing, use the ls -lZ command to view the SELinux permissions of the /var/www directory. The type needs to be httpd_sys_content_t.

~]# ls -ldZ /var/www
-r--r--r-- 1 root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www

 

If the type is not httpd_sys_content_t, use apt-get, dnf, or yum to install policycoreutils-python. This package contains semanage.

~]# dnf -y install policycoreutils-python

 

The semanage command can then be used to set the /var/www directory to have SELinux type httpd_sys_content_t. This is a permanent change, meaning this setting will remain in tact after the system is rebooted.

~]# semanage fcontext -a -t httpd_sys_content_t /var/www

 

The restorecon command can then be used to update every file and directory below /var/www to have SELinux type httpd_sys_content_t.

~]# restorecon -Rv /var/www

 

On a PC in the same subnet as the web server, navigate to http://x.x.x.x (replace x.x.x.x with the IP address of the web server). The default page should be displayed.

 

The /etc/nginx/nginx.conf file is configured to serve files in the /usr/share/nginx/html directory. The index.html file in the /usr/share/nginx/html directory produces the screen shot above.

[root@server1 ~]# cat /etc/nginx/nginx.conf
. . .
server {
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  _;
  root         /usr/share/nginx/html;
. . .

 

In the /etc/nginx/nginx.conf file, add your domain name to the server_name line. In this example, the server_name is example.com. Also add index index.html to ensure that index.html files are served by default.

[root@server1 ~]# cat /etc/nginx/nginx.conf
. . .
server {
  listen       80 default_server;
  listen       [::]:80 default_server;
  server_name  www.example.com;
  root         /usr/share/nginx/html;
  index        index.html;
. . .

 

Restart nginx, and ensure nginx is active and running.

[root@server1 ~]# systemctl start nginx
[root@server1 ~]# systemctl status nginx

 

You can now access your site using your domain name.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 08f7e3 in the box below so that we can be sure you are a human.