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
root@ubuntu-2204:~# mkdir /var/www/
root@ubuntu-2204:~# nano /var/www/
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
   <h1><center>Virtual Host - Ubuntu 22.04</h1>
   <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>
  • Buat file config virtual host untuk domain
root@ubuntu-2204:~# nano /etc/nginx/conf.d/
server {
   listen 80;
   root /var/www/;

   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 pada browser


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