Cara setup Nginx server block di CentOS

By | August 6, 2015

Panduan cara membangun virtual host Nginx (server block) di Linux CentOS. Di panduan ini, selaku contoh kita akan menambahkan domain idnetter.com dan idnetter.net. Jadi intinya satu server dapat bagi meng-host banyak domain. Berikut langkah-langkahnya.

Menambahkan repository EPEL

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

Centos 7

yum install epel-release

CentOS 6 32-Bit

wget http://unduh.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://unduh.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 tak, kalau tak silahkan periksa pengaturan firewall.

Pengaturan virtual host Nginx

File konfigurasi yang akan dimenej dan dibangun ialah selaku berikut

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

Modifikasi konfigurasi PHP-FPM

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

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

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

Modifikasi Nginx.conf

Modifikasi 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 informasi;

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;

# Bagi 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/*;


}

Merancang folder penyimpanan file konfigurasi virtual host

Bikin direktori baru di pada /etc/nginx/ dengan nama sites-available dan sites-enabled.

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

Merancang file konfigurasi virtual host

Agar terstruktur, setiap domain akan mempunyai file konfigurasi sendiri. Beri nama file konfigurasi sesuai dengan nama domain agar gampang pada mengelolanya. Ingat, file mesti berakhiran .conf.

Contoh:

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

Bikin 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 tema berikut, ganti yang tanda merah sesuaikan dengan versi/lokasi PHP-FPM Kamu.

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 informasi versi
fastcgi_hide_header X-Powered-By;

}
}

Berikutnya, bikin file konfigurasi bagi 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 informasi versi
fastcgi_hide_header X-Powered-By;

}
}

Bikin 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, menej file yang berada di pada /etc/nginx/sites-avaiable/ agar setiap kita mengedit, kita dapat mengetesnya terlebih dahulu sebelum konfigurasi direload.

test dengan menjalankan petunjuk berikut

nginx -t

kalau telah OK, baru di reload

service nginx reload
service php-fpm reload

Bikin folder web root bagi 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}

Bikin file index.php bagi mengerjakan 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 seluruh file da folder yang terdapat di direktori root tersebut

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

Selesai, selamat mencoba ?

 


Sumber https://idnetter.com