Deploy Nginx di Ubuntu

Dalam beberapa hari belakangan ini, saya beberapa kali melakukan deploy aplikasi / web server Nginx di server linux terutama di Ubuntu. Dan pada kesempatan kali ini saya akan membahas tentang artikel tersebut, kurang lebihnya untuk melakukan pointing web server menggunakan nginx / apache konsepnya hampir sama. Konfigurasi yang akan saya lakukan adalah melalui root directory.

Konfigurasi Deploy Web Server Nginx di Ubuntu sebagi berikut :

Install Dependency

1. Install Nginx
$sudo apt update
$sudo apt install nginx
  • Install PHP FPM
apt install php7.4-fpm php7.4-common php7.4-xml php7.4-zip php7.4-mysql php7.4-mbstring php7.4-json php7.4-curl php7.4-gd php7.4-pgsql 
2. Check web server
  • Check status nginx
$systemctl status nginx
  • output
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-04-20 16:08:19 UTC; 3 days ago
     Docs: man:nginx(8)
 Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
   Memory: 3.5M
   CGroup: /system.slice/nginx.service
           ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2380 nginx: worker process

Status nginx running

  • Testing akses web server menggunakan IP anda
  • check bila menggunakan curl $curl http://your_ip check menggunakan browser http://your_ip
3. Manage Nginx proses
  • Stop nginx
$ sudo systemctl stop nginx
  • Start nginx
$ sudo systemctl start nginx
  • Restart nginx
$ sudo systemctl restrart nginx
  • Reload nginx (restart service tanpa mengganggu conection web server)
$ sudo systemctl reload nginx
  • Disable service nginx
$ sudo systemctl disable nginx
  • Enable service nginx
$ sudo systemctl enable nginx
4. Setup Permissions file directory web server
  • Upload source code / create folder baru
$ sudo mkdir -p /var/www/html/example
  • Permissions file directory tsb
$ sudo chown -R $USER:$USER /var/www/html/example
$ sudo chmod -R 755 /var/www/html/example
  • NB: Untuk permission storage & vendor menggunakan 777

  • Permssion file directory example
$ sudo chown -R www-data:www-data /var/www/html/example
5. Setting Document Root
  • masuk ke folder nginx loc di cd
$ cd /etc/nginx/sites-available/
  • Create configuration nginx dengan buat file example.conf
$ sudo mkdir -p example.conf
  • Edit/isikan file example.conf dengan script seperti dibawah ini (berbasis laravel) :
server {
        root /var/www/html/example/public;
        index index.php index.html;
        server_name example.com;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location ~ /\.ht {
                deny all;
        }
        location ~ /\.env {
                deny all;
        }
        location ~ /\.git {
                deny all;
        }
  • Enable configuration
$sudo cd /etc/nginx/sites-enabled
$sudo ln -s /etc/nginx/sites-available/example.conf ./

NB : unlink file default.confnya , $ sudo unlink -s /etc/nginx/sites-enabled/default

  • Testing syntax nginx.conf yang kita buat
$ sudo nginx -t
  • Restart nginx
$sudo systemctl restart nginx

Kemudian akses kembali web server anda menggunakan IP atau domain, selamat mencoba.

Leave a Reply

Your email address will not be published. Required fields are marked *