Nginx is available in the pre-installed Ubuntu package repositories. You can install it with the following command:<\/p>\n
$ apt install nginx<\/pre>\nOnce the installation is complete, Nginx will be automatically started.
\nWe can make sure that the Nginx service is running with the following command:<\/p>\n
$ systemctl status nginx<\/pre>\nThe output should look similar to the one found below:<\/p>\n
\u25cf nginx.service - A high performance web server and a reverse proxy server\r\n Loaded: loaded (\/lib\/systemd\/system\/nginx.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Sat 2019-01-21 01:50:44 CDT; 19s ago\r\n Main PID: 619 (nginx)\r\n CGroup: \/system.slice\/nginx.service\r\n \u251c\u2500619 nginx: master process \/usr\/sbin\/nginx -g daemon on; master_process on\r\n \u251c\u2500620 nginx: worker process\r\n \u251c\u2500621 nginx: worker process\r\n \u251c\u2500622 nginx: worker process\r\n \u2514\u2500623 nginx: worker process\r\n<\/pre>\n<\/span>3: Managing the Nginx Service<\/span><\/h2>\nEnable the Nginx server at boot time using the systemctl command:<\/p>\n
$ systemctl enable nginx<\/pre>\nStart Nginx server using the systemctl command:<\/p>\n
$ systemctl start nginx<\/pre>\nRestart Nginx server using the systemctl command:<\/p>\n
$ systemctl restart nginx<\/pre>\nStop Nginx server using the systemctl command:<\/p>\n
$ systemctl stop nginx<\/pre>\nReload Nginx server using the systemctl command:<\/p>\n
$ systemctl reload nginx<\/pre>\nGet the status of the Nginx server using the systemctl command:<\/p>\n
$ systemctl status nginx<\/pre>\n<\/span>4: Creating a New Server Block<\/span><\/h2>\nThe default Nginx installation will have one server block enabled with a document root set to \/var\/www\/html<\/code>.
\nIn this guide, we will create a new server block for the domain your_domain.com<\/code> and set the document root to \/var\/www\/your_domain.com<\/code>.<\/p>\nFirst, create the domain’s document root directory with the following command:<\/p>\n
$ mkdir -p \/var\/www\/your_domain.com<\/pre>\nand then create an index.html<\/code> file with the following content:<\/p>\n$ nano \/var\/www\/your_domain.com\/index.html<\/pre>\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>your_domain.com<\/title>\r\n<\/head>\r\n<body>\r\n <h1>your_domain.com server block<\/h1>\r\n<\/body>\r\n<\/html>\r\n<\/pre>\nNext, create a new server block with the following content by creating a file in the directory \/etc\/nginx\/sites-available<\/code>:<\/p>\n$ nano \/etc\/nginx\/sites-available\/your_domain.com.conf<\/pre>\nserver {\r\n listen 80;\r\n listen [::]:80;\r\n\r\n server_name your_domain.com www.your_domain.com<\/code>; root \/var\/www\/your_domain.com; index index.html; location \/ { try_files $uri $uri\/ =404; } }<\/pre>\nOnce you are done, save the file and close it.<\/p>\n
Activate the server block by creating a symbolic link:<\/p>\n
$ ln -s \/etc\/nginx\/sites-available\/your_domain.com.conf \/etc\/nginx\/sites-enabled\/your_domain.com.conf<\/pre>\nCheck if there are any syntax errors present in the Nginx configuration by using the command below:<\/p>\n
$ nginx -t<\/pre>\nIf everything is OK with the configuration, the output should be similar to the one below:<\/p>\n
$ nginx -t\r\nnginx: the configuration file \/etc\/nginx\/nginx.conf syntax is ok\r\nnginx: configuration file \/etc\/nginx\/nginx.conf test is successful<\/pre>\nOtherwise, the errors will be listed in the output so you easily find out what the problem is.<\/p>\n
Once you are sure there are no problems with the configuration in Nginx, you can restart the service using the following command:<\/p>\n
$ systemctl restart nginx.service<\/pre>\n