how-to-install-and-set-up-nginx-on-ubuntu-and-centos

How to Install and Set Up Nginx on Ubuntu and CentOS

About Nginx

Nginx (pronounced “engine X”), stylized as NGINX or nginx or NginX, is a high-performance web server software that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software.

In this post, I will note commands that use to install and setup Nginx on Ubuntu 18 or CentOS 7 server.

Install Nginx

The first step is to install Nginx and verify the installation. You can do it with this command:

Ubuntu

sudo apt-get update
sudo apt-get install nginx
sudo nginx -v

CentOS

sudo yum install epel-release
sudo yum update
sudo yum install nginx
sudo nginx -v

If you want to uninstall Nginx, for any reason, you can do that with the following commands:

Ubuntu

Uninstall Nginx but save the configuration:

sudo apt-get remove nginx-common

Uninstall Nginx completely:

sudo apt-get purge nginx nginx-common
sudo apt-get autoremove

CentOS

Remove Nginx package from the CentOS 7 server:

sudo yum remove nginx

Remove the Nginx data directory:

sudo rm -R /etc/nginx
sudo rm -R /var/log/nginx

Control Nginx

After that you verify Nginx install, you can control it with this command:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl status nginx

If you want to make sure that the Nginx will be started at boot, run this command:

sudo systemctl enable nginx

Finally, you can verify that Nginx is up and running from the server with this command:

curl -I 127.0.0.1

Configuration Server Block for Nginx

To start serving your own pages or application through Nginx, first, you should know about the locations of the Nginx global and server blocks configuration files and default server root directory.

Nginx Global Configuration

The main Nginx configuration file is located at /etc/nginx/nginx.conf. This is where you can change settings and in this file, you can see the server block that configures the Nginx default page like this:

server {
	listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

	error_page 404 /404.html;
                location = /404.html {
        }

	error_page 500 502 503 504 /50x.html;
                location = /50x.html {
        }
}

Nginx Server Blocks Configuration

Any additional server blocks can be added by creating new configuration files in /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/. Files that end with .conf in these directories will be loaded when Nginx is started.

But often you can’t find these directories after install Nginx, so you should make them yourself. before that let’s decide to use /conf.d/ or /sites-enabled/ and /sites-available/.

The sites-available folder is for storing all of your vhost configurations, whether or not they’re currently enabled.

The sites-enabled folder contains symlinks to files in the sites-available folder. This allows you to selectively disable vhosts by removing the symlink.

conf.d does the job, but you have to move something out of the folder, delete it, or make changes to it when you need to disable something. The sites-* folder abstraction makes things a little more organized and allows you to manage them with separate support scripts, so I offer to use sites-available and sites-enabled.

Default Server Root

The default server root directory is /usr/share/nginx/html. Files that are placed in there will be served on your web server. This location is specified in the default server block configuration file that ships with Nginx, which is located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.

What do we do?

If you are confused a little like me that what directory use for website static files and how to configure server blocks, I have to say I found the below approach is useful after a lot of search about this subject:

1. Edit the /etc/nginx/nginx.conf file and comment out the server block in this file. Then add these lines after include /etc/nginx/conf.d/*.conf;.

include /etc/nginx/sites-enabled/*.conf
server_names_hash_bucket_size 64;

2. If there are not the /etc/nginx/sites-available and /etc/nginx/sites-enabled directories, make them with these commands:

mkdir /etc/nginx/sites-available/
mkdir /etc/nginx/sites-enabled/

3. Create a new file specifically for the server block for the yourdomain.com site and write into the file the below server block.

nano /etc/nginx/sites-available/yourdomain.com.conf
server {
	listen 80;
        listen [::]:80;
	
	# server_name example.com www.example.com; # dns server name
 
	# log files
	access_log  /var/log/nginx/yourdomain.com.conf_access.log;
	error_log   /var/log/nginx/yourdomain.com.conf_error.log;
 
	# document root where files stores for project
	root /{directory-of-static-files}/;
	index index.html index.htm;

	location / {
                try_files $uri $uri/ /index.html =404;
        }
}

4. creating a link from the above file to the sites-enabled:

sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/

5. Make the directory of website static files that you introduce in the server block in step 3 and copy the files into it:

sudo mkdir -p /{directory-of-static-files}
sudo chown -vR $USER:$USER /{directory-of-static-files}
sudo chmod -R 755 /{directory-of-static-files}

6. Test the Nginx configuration for correct syntax:

sudo nginx -t

If there are no errors, the output will look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

7. Restart the Nginx service for the changes to take effect and make sure that the Nginx will be started at boot:

sudo systemctl restart nginx
sudo systemctl enable nginx

Finally, to verify the server block is working as expected open http://ip-address in your browser.

Configuration Firewall

Don’t remember that we need to open the port that we want to use, so we have to go to firewall setup and change the configuration. I published a post for the firewall set up in Ubuntu and CentOS before, you can read it first.

404 Permission Denied

If you see permission denied after verifying the permissions of the parent folders, it may be SELinux restricting access.

To check if SELinux is running:

getenforce

To disable SELinux until the next reboot:

setenforce Permissive

Restart Nginx and see if the problem persists. To allow Nginx to serve your www directory (make sure you turn SELinux back on before testing this. i.e, setenforce Enforcing)

chcon -Rt httpd_sys_content_t /{directory-of-static-files}

For more details see this.

You Might Also Like
1 Comment
Leave a Reply to IMer Cancel reply