Install, launch Nginx and enable to start on boot
This archive is written for CentOS 7.x
1
2
3
|
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
|
Verify that Nginx is running
1
|
sudo systemctl status nginx
|
1
2
3
4
5
6
7
8
9
10
11
12
|
● 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 Sat 2022-09-10 05:41:32 UTC; 1 weeks 0 days ago
Process: 76789 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 76787 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 76786 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 76791 (nginx)
Tasks: 2 (limit: 10838)
Memory: 4.6M
CGroup: /system.slice/nginx.service
├─76791 nginx: master process /usr/sbin/nginx
└─76792 nginx: worker process
|
1
2
3
|
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
|

Serve a web app with Nginx
Create directory for web app files, assuming the web app is going to be served with the domain name hainguyen.dev
:
1
|
sudo mkdir -p /var/www/hainguyen.dev/html
|
Change ownership of the directory to the current user:
1
|
sudo chown -R $USER:$USER /var/www/hainguyen.dev/html
|
$USER:$USER is the current user and group. If you want to change the ownership to a different user, replace $USER with the username.
1
2
3
|
Change the permissions of the directory to allow read, write and execute for the owner:
```shell
sudo chmod -R 755 /var/www
|
Create an index.html file in the directory:
1
|
vi /var/www/hainguyen.dev/html/index.html
|
1
2
3
4
5
6
7
8
9
|
<!DOCTYPE html>
<html>
<head>
<title>hainguyen.dev</title>
<head>
<body>
<h1>Hello, World!, Welcome to my website.</h1>
<body>
<html>
|
Create a new server block file for the domain in the Nginx sites-available directory:
1
2
|
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
|
Update the Nginx configuration file to include the sites-enabled directory:
1
|
sudo vi /etc/nginx/nginx.conf
|
Add the following lines to the http block:
First line to tell Nginx to look for server block files in the sites-enabled directory.
Second line to increase the size of the server_names_hash_bucket_size to avoid errors when using long domain names.
1
2
|
include /etc/nginx/sites-enabled/*.conf
server_names_hash_bucket_size 64;
|
The nginx.conf file should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
server {
listen 80 default_server;
listen [::]:80;
server_name _;
return 444;
}
}
|
Note: The return 444 line tells Nginx to return a 444 status code to any requests that do not match a server block. This is useful if you are using Nginx as a reverse proxy and do not want it to respond to requests for domains that it is not configured to serve.
In this case, we only want Nginx to respond to requests for the domain we are configuring, and we want to hide the default server page so we return a 444 status code for the requests at default_server.
Create server block for the domain
1
|
sudo vi /etc/nginx/sites-available/hainguyen.dev.conf
|
1
2
3
4
5
6
7
8
9
10
|
server {
listen 80;
server_name hainguyen.dev www.hainguyen.dev;
location / {
root /var/www/hainguyen.dev/html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
}
|
Enable the server block
1
|
sudo ln -s /etc/nginx/sites-available/hainguyen.dev.conf /etc/nginx/sites-enabled/hainguyen.dev.conf
|
Test Nginx configuration
Output:
1
2
|
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
|
Restart Nginx
1
|
sudo systemctl restart nginx
|
Verify that the web app is accessible
1
|
curl http://hainguyen.dev
|
Output:
1
2
3
4
5
6
7
8
9
10
|
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<title>hainguyen.dev</title>
<head>
<body>
<h1>Hello, World!, Welcome to my website.</h1>
<body>
<html>
|
1
|
curl http://server_ip_address
|
Output:
1
|
curl: (52) Empty reply from server
|