Setting Server Block Nginx (Virtual Hosts) on Ubuntu 22.04

  • Install nginx
root@ubuntu-2204:~# apt update && apt install nginx -y
  • Buat directory dan file index html untuk domain nginx.rainui.cloud
root@ubuntu-2204:~# mkdir /var/www/nginx.rainui.cloud/
root@ubuntu-2204:~# nano /var/www/nginx.rainui.cloud/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>sys-ops.id</title>
  </head>
  <body>
   <h1><center>Virtual Host nginx.rainui.cloud - Ubuntu 22.04</h1>
   <h3><center>Sys-Ops.id</h3>
   <p>
   <li>install nginx</li>
   <li>setting nginx virtual host</li>
   <li>install https let's encrypt</li>
   <li>install php 8.0</li>
   <li>install mariadb 10.6</li>
   <li>install wordpress</li>
  </body>
</html>
  • Buat file config virtual host untuk domain nginx.rainui.cloud
root@ubuntu-2204:~# nano /etc/nginx/conf.d/nginx.rainui.cloud.conf
server {
   listen 80;
   server_name nginx.rainui.cloud;
   root /var/www/nginx.rainui.cloud;

   location / {
       index index.php index.html index.htm;
       try_files $uri $uri/ /index.php?$args;
   }

         add_header X-Frame-Options "SAMEORIGIN";
         add_header X-Content-Type-Options "nosniff";
         add_header X-Xss-Protection "1; mode=block";
         add_header Referrer-Policy strict-origin-when-cross-origin;
         add_header Access-Control-Allow-Origin "*";
         add_header Strict-Transport-Security "max-age=15552000; includeSubDomains;preload";

         location ~* /\.(?!well-known\/) {
                 deny all;
         }

         location ~\.(ini|log|conf)$ {
                 deny all;
         }

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php/php8.1-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_read_timeout 300;
      proxy_read_timeout 600;
   }
}
  • Restart service nginx
root@ubuntu-2204:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

root@ubuntu-2204:~# systemctl restart nginx
  • Akses server block domain nginx.rainui.cloud pada browser

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.