14. Setup NGINX for several sites and test sub domains with NGINX server blocks
Check so NGINX is allowed in the firewall
sudo ufw status
If not, allow NGINX with below command
sudo ufw allow 'Nginx HTTP'
Check so NGINX is installed correctly and active
systemctl status nginx
To start the NGINX server when it is stopped, type:
sudo systemctl start nginx
To stop your NGINX server, type:
sudo systemctl stop nginx
To stop and then start the service again, type:
sudo systemctl restart nginx
If you are only making configuration changes, Nginx can often reload without dropping connections. To do this, type:
sudo systemctl reload nginx
Nginx on Ubuntu 20.04 has one server block enabled by default
that is configured to serve documents out of a directory at
/var/www/html.
While this works well for a single site, it can become unwieldy
if you are hosting multiple sites. Instead of modifying
/var/www/html,
let’s create a directory structure within /var/www for our {{tutorial.yourdomain}} site,
leaving /var/www/html in place as the default directory to
be served if a client request doesn’t match any other sites.
Create content directories for your web sites {{tutorial.yourdomain}} and {{tutorial.your2nddomain}}, and also for your test sites test.{{tutorial.yourdomain}} and test.{{tutorial.your2nddomain}}, so 4 directories:
sudo mkdir -p /var/www/{{tutorial.yourdomain}}/html
sudo mkdir -p /var/www/{{tutorial.yourdomain}}/test
sudo mkdir -p /var/www/{{tutorial.your2nddomain}}/html
sudo mkdir -p /var/www/{{tutorial.your2nddomain}}/test
Assign ownership of the directories with the $USER environment variable:
sudo chmod -R 755 /var/www/{{tutorial.yourdomain}}
sudo chmod -R 755 /var/www/{{tutorial.your2nddomain}}
Add some code:
sudo nano /var/www/{{tutorial.yourdomain}}/html/index.html
Add some sample HTML
Change ownership from root to your new sudouser
sudo chown -R {{tutorial.sudouser}} /var/www/
You can also create new users for the second domain, and even for the test domains if you want to limit access to certain team members or customers.
If we want any regular non-root user to be able to modify files in these directories, we can change the ownership to "whoami".
sudo chown -R $(whoami):$(whoami) /var/www/{{tutorial.yourdomain}}/html
sudo chown -R $(whoami):$(whoami) /var/www/{{tutorial.your2nddomain}}/html
The $(whoami) variable will take the value of the user you are currently logged in as.
Create a blank NGINX config file
sudo nano /etc/nginx/sites-available/{{tutorial.yourdomain}}
to paste below code into:
# server_name {{tutorial.yourdomain}} www.{{tutorial.yourdomain}} test.{{tutorial.yourdomain}};
server {
listen 80;
server_name www.{{tutorial.yourdomain}};
# www to non-www for SEO canonical reasons
return 301 http://{{tutorial.yourdomain}}$request_uri;
}
server {
listen 80;
server_name {{tutorial.yourdomain}};
root /var/www/{{tutorial.yourdomain}}/html;
index index.html index.htm index.php index.nginx-debian.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|webp)$
{
expires 24h; # add_header Cache-Control private;
add_header Cache-Control no-store;
}
# For WordPress permalinks
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
# All below for test subdomain
server {
listen 80;
server_name test.{{tutorial.yourdomain}};
root /var/www/{{tutorial.yourdomain}}/html;
index index.html index.htm index.php index.nginx-debian.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|webp)$
{
expires 24h; # add_header Cache-Control private;
add_header Cache-Control no-store;
}
# For WordPress permalinks
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Use CTRL C to close and save.
sudo nginx -t
(check for errors)
sudo systemctl reload nginx
Now that we have our first nginx server block configuration, we can use that as a basis for our second file. Copy it over to create a new file:
sudo cp /etc/nginx/sites-available/{{tutorial.yourdomain}} /etc/nginx/sites-available/{{tutorial.your2nddomain}}
Adjust the root directive to point to our second domain's document root and adjust the server_name to match your second site's domain name:
sudo nano /etc/nginx/sites-available/{{tutorial.your2nddomain}}
Now that we have our server block files, we need to enable them. We can do this by creating symbolic links from these files to the sites-enabled directory, which Nginx reads from during startup.
We can create these links by typing:
sudo ln -s /etc/nginx/sites-available/{{tutorial.yourdomain}} /etc/nginx/sites-enabled/
To avoid a possible hash bucket memory problem that can arise from adding additional server names, we will go ahead and adjust a single value within our /etc/nginx/nginx.conf file. Open the file now with:
sudo nano /etc/nginx/nginx.conf
Uncomment below line and change to:
server_names_hash_bucket_size 128;
Also add below lines:
client_max_body_size 128M;
server_tokens off;
(hides Nginx server version)
You can also uncomment all lines in the Gzip paragraph.
Save and close the file when you are finished.
Next, test to make sure that there are no syntax errors in any of our Nginx files:
sudo nginx -t
If no problems were found, restart Nginx to enable our changes:
sudo systemctl restart nginx
You can already test if your web sites are working by clicking
http://{{tutorial.yourdomain}}, http://{{tutorial.your2nddomain}},
http://test.{{tutorial.yourdomain}}, and http://test.{{tutorial.your2nddomain}}
You can edit your pages with Nano SSH editor
sudo nano /var/www/{{tutorial.yourdomain}}/html/index.html
sudo nano /var/www/{{tutorial.your2nddomain}}/html/index.html
sudo nano /var/www/{{tutorial.yourdomain}}/test/index.html
sudo nano /var/www/{{tutorial.your2nddomain}}/test/index.html
Improve your NGINX Server's Performance and Security by following below tutorial after setting up SLL/https.