Cara setup Nginx server block di CentOS

Panduan cara membuat virtual host Nginx (server block) pada Linux CentOS. Pada tutorial ini, sebagai contoh kita akan menambahkan domain idnetter.com dan idnetter.net. Jadi intinya satu server bisa untuk meng-host banyak domain. Berikut langkah-langkahnya.

Menambahkan repository EPEL

Pertama tambahkan repo dari epel, sesuaikan dengan versi Linux CentOS yang digunakan:

Centos 7

yum install epel-release

CentOS 6 32-Bit

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm && rpm -ivh epel-release-6-8.noarch.rpm

CentOS 6 64-Bit

wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && rpm -ivh epel-release-6-8.noarch.rpm

Hapus Apache (HTTPD)

Hapus Apache bawaan CentOS

service httpd stop && yum -y remove httpd

Install Nginx

yum install nginx -y && chkconfig nginx on 

Aktifkan Nginx

service nginx start

Setelah Nginx diaktifkan, cek dengan browser apakah IP server dapat diakses atau tidak, kalau tidak silahkan periksa pengaturan firewall.

Pengaturan virtual host Nginx

File konfigurasi yang akan diedit dan dibuat adalah sebagai berikut

  1. /etc/php-fpm.d/www.conf
  2. /etc/nginx/nginx.conf
  3. /etc/nginx/sites-available/*.conf

Edit konfigurasi PHP-FPM

nano /etc/php-fpm.d/www.conf

Sesuaikan yang saya tandai dengan versi PHP-FPM yang Anda gunakan.

user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx

Edit Nginx.conf

Edit file /etc/nginx/nginx.conf dengan editor nano

nano /etc/nginx/nginx.conf

Tambahkan baris kode tang saya tandai warna merah

user              nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    #keepalive_timeout  0;
    keepalive_timeout  30;
    
    # Untuk perfoma nginx terbaik
    gzip on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;

    # Meload konfigurasi dari direktory sites-enabled
    include /etc/nginx/sites-enabled/*;

}

Membuat folder penyimpanan file konfigurasi virtual host

Buat direktori baru di dalam /etc/nginx/ dengan nama sites-available dan sites-enabled.

mkdir /etc/nginx/sites-{available,sites-enabled}

Membuat file konfigurasi virtual host

Agar terstruktur, setiap domain akan mempunyai file konfigurasi sendiri. Beri nama file konfigurasi sesuai dengan nama domain agar mudah dalam mengelolanya. Ingat, file harus berakhiran .conf.

Contoh:

  • idnetter.com.conf artinya ini untuk konfigurasi domain idnetter.com
  • idnetter.net.conf artinya untuk domain idnetter.net dan seterusnya.

Buat sebuah file baru didalam folder /etc/nginx/sites-available/ dengan nama idneetter.com.conf via nano teks editor.

nano /etc/nginx/sites-available/idntter.com.conf

Isi dengan template berikut, ganti yang tanda merah sesuaikan dengan versi/lokasi PHP-FPM Anda.

server {
        listen 80;
		listen [::]:80;
        server_name idnetter.com www.idnetter.com;
 
        location /error/ {
           alias   /usr/share/nginx/html/errors/;
        }
 
        root /var/www/html/idnetter.com/;
        index index.htm index.html index.php;
 
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
 
        access_log /var/log/nginx/access_log;
        access_log off;
        error_log /var/log/nginx/error_log error;
 
        # security
        error_page 403 = 404;
        location ~ /\. { access_log off; log_not_found off; deny all; }
        location ~ ~$ { access_log off; log_not_found off; deny all; }
 
        location = /robots.txt { access_log off; log_not_found off; }
        location = /favicon.ico { access_log off; log_not_found off; }
 
        # caches
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; }
        location ~* \.(woff|svg)$ { access_log off; log_not_found off; expires 30d; }
        location ~* \.(js)$ { access_log off; log_not_found off; expires 7d; add_header Cache-Control "public, no-transform"; }
 
 
        # php block
        location ~ \.php?$ {
		try_files $uri = 404;
		include fastcgi_params;
		fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_intercept_errors on;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
 
		# Cegah kebocoran info versi
		fastcgi_hide_header X-Powered-By;
 
        }
}

Selanjutnya, buat file konfigurasi untuk domain idnetter.net dengan nama idnetter.net.conf cara yang sama hanya saja bagian server_name diganti.

nano /etc/nginx/sites-available/idnetter.net.conf

Isi dengan kode baris berikut

server {
        listen 80;
		listen [::]:80;
        server_name idnetter.net www.idnetter.net;
 
        location /error/ {
           alias   /usr/share/nginx/html/errors/;
        }
 
        root /var/www/html/idnetter.net/;
        index index.htm index.html index.php;
 
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
 
        access_log /var/log/nginx/access_log;
        access_log off;
        error_log /var/log/nginx/error_log error;
 
        # security
        error_page 403 = 404;
        location ~ /\. { access_log off; log_not_found off; deny all; }
        location ~ ~$ { access_log off; log_not_found off; deny all; }
 
        location = /robots.txt { access_log off; log_not_found off; }
        location = /favicon.ico { access_log off; log_not_found off; }
 
        # caches
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; }
        location ~* \.(woff|svg)$ { access_log off; log_not_found off; expires 30d; }
        location ~* \.(js)$ { access_log off; log_not_found off; expires 7d; add_header Cache-Control "public, no-transform"; }
 
 
        # php block
        location ~ \.php?$ {
		try_files $uri = 404;
		include fastcgi_params;
		fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_intercept_errors on;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
 
		# Cegah kebocoran info versi
		fastcgi_hide_header X-Powered-By;
 
        }
}

Buat symbolic link ke sites-enabled

ln -s /etc/nginx/sites-available/idnetter.com.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/idnetter.net.conf /etc/nginx/sites-enabled/

Jadi kalau mau modifikasi, edit file yang berada di dalam /etc/nginx/sites-avaiable/ agar setiap kita mengedit, kita bisa mengetesnya terlebih dahulu sebelum konfigurasi direload.

test dengan menjalankan perintah berikut

nginx -t

kalau sudah OK, baru di reload

service nginx reload
service php-fpm reload

Buat folder web root untuk menyimpan file-file dari setiap domain. dari Konfigurasi di atas dokumen root domain idnetter.com dan idnetter.net berada di /var/www/html, jadi

mkdir /var/www/html/{idnetter.com,idnetter.net}

Buat file index.php untuk melakukan uji coba

echo "Halo idnetter.com " | sudo tee /var/www/html/idnetter.com/index.php
echo "halo idnetter.net " | sudo tee /var/www/html/idnetter.net/index.php

Jangan lupa ubah hak akses semua file da folder yang ada di direktori root tersebut

chown -R nginx:nginx /var/www/html/*

Selesai, selamat mencoba 😀

 

2 thoughts on “Cara setup Nginx server block di CentOS

  1. moko

    Mas, kalau mau setting email domain sendiri di vps bisa tidak ya? Jadi akses emailnya bisa unlimitted dan cepat, karena sekarang sy menggunakan email domain sendiri menggunakan cpanel shared hosting,, jadi kirim dan terima email sering tertunda.

    Terima kasih

    1. Gung Web

      bisa gan, jangan lupa atur juga SPF & DKIM nya. kl pakai Webuzo lihat tutorialnya disini

Add a comment