Cara install LEMP di CentOS 6

Tutorial cara install LEMP (Linux, Nginx, MySQL, PHP) di Linux CentOS 6. Pilihan panduan LEMP server untuk OS versi lain juga tersedia, diantaranya:

Langkah-langkah install LEMP server di Linux CentOS 6

1. Tambahkan repository EPEL

Pertama tambahkan repo dari epel. silahkan disesuaikan dengan versi Linux CentOS yang digunakan:

RHEL/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

RHEL/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

2. Hapus HTTPD

Stop dan hapus dulu paket Apache bawaan CentOS 6, agar nanti tidak terjadi konflik pada port 80 yang akan digunakan oleh Nginx (Engine-X).

service httpd stop
yum remove httpd

3. Instalasi MySQL

Install MySQL dengan kode berikut:

yum -y install mysql mysql-server
chkconfig --levels 235 mysqld on

Start MySQL dengan perintah berikut

service mysqld start
mysql_secure_installation

Ketika diminta untuk memasukkan password pertama kali abaikan saja, langsung tekan Enter.

Kemudian Jika ada pertanyaan membuat password pilih Y kemudian enter dan ketik password yang anda inginkan. Pertanyaan selanjutnya jawab dengan Y kemudian Enter sampai instalasi selesai / berhasil:

4. Instalasi PHP

Perintah berikut secara otomatis akan meng-install PHP beserta modul-modul penting yang diperlukan untuk membangun website, seperti mengirim email, manipulasi gambar, sistem caching dan-lain-lain.

yum -y install php-fpm php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy curl curl-develphp-pecl-apc

Jalankan perintah berikut agar service php-fpm selalu auto start jika mesin direstart.

chkconfig php-fpm --levels 235 on

Edit konfigurasi php.ini

vi /etc/php.ini

Cari line berikut, hilangkan tanda komen # dan ubah valuenya seperti berikut:

cgi.fix_pathinfo = 0
date.timezone = Asia/Jakarta
expose_php = Off

5. Instalasi Nginx

yum install nginx -y
chkconfig --levels 235 nginx on

Setelah proses selesai, start Nginx

service nginx start

Buka browser coba akses IP server, hasilnya kira-kira seperti ini

cara install nginx di centos

6. Konfigurasi virtual host Nginx

File konfigurasi Nginx yang akan diedit adalah sebagai berikut

  • /etc/nginx/nginx.conf
  • /etc/nginx/conf.d/default.conf
  • /etc/php-fpm.d/www.conf

Edit konfigurasi Nginx

vi /etc/nginx/nginx.conf

Edit konfigurasinya, aktifkan gzip atau sesuaikan seperti berikut:

user              nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
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  30;

    # Aktifkan kompresi gzip
    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;

    # Direktori untuk file konfigurasi virtual host
    include /etc/nginx/conf.d/*.conf;

}

Edit virtual host Nginx

echo > /etc/nginx/conf.d/default.conf && vi /etc/nginx/conf.d/default.conf

Berikutnya, copy-paste teks berikut

server {
	listen 80;
	server_name idnetter.com;

	client_max_body_size 5m;
	client_body_timeout 60;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log error;
	
	root /var/www/html/;
	index index.php index.htm index.html;

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

	# 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; }
	
	# Disable logging
	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; }
    
	# Hapus block ini jika Anda tidak memakai WordPress
	# Start WordPress
	location ~* wp-admin/includes { deny all; }
	location ~* wp-includes/theme-compat/ { deny all; }
	location ~* wp-includes/js/tinymce/langs/.*\.php { deny all; }
	location /wp-includes/ { internal; }
	location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
	        types { }
	        default_type text/plain;
	}
	# End WordPress
	
	# Hapus jika tidak diperlukan
	# Start contoh URL rewrite Nginx
	# Hasilnya: http://idnetter.com/page-2
	# rewrite ^/page-([0-9]+)/?$ /index.php?page=$1 last;
	# Hasilnya: http://idnetter.com/artikel/sesuatu/
	# rewrite ^/artikel/([^/]*)/?$ /artikel.php?q=$1 last;
	# rewrite ^/artikel/([^/]*)/page-([0-9]+)/$ /artikel.php?q=$1&page=$2 last;
	# End URL rewrite

	# PHP
	location ~ \.php?$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_hide_header X-Powered-By;
	}
}

Edit file /etc/php-fpm.d/www.conf.

echo > /etc/php-fpm.d/www.conf vi /etc/php-fpm.d/www.conf

Sesuaikan versi PHP, defaultnya saat install adalah versi 5

[www]
listen = /var/run/php5-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
user = nginx
group = nginx
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/php5-fpm.log
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 400
listen.backlog = -1
pm.status_path = /status
request_terminate_timeout = 120s
rlimit_files = 131072
rlimit_core = unlimited
catch_workers_output = yes
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_admin_value[error_log] = /var/log/php-fpm/php5-fpm.log
php_admin_flag[log_errors] = on

Simpan dan keluar dari vim editor Ctrl+C > ketik :wq > tekan Enter

Restart Nginx dan PHP-FPM

service nginx restart && service php-fpm restart

Bua file info.php

vi /var/www/html/coklat/info.php

Isi dengan kode berikut:

<?php phpinfo();

Simpan.

Ubah perizinan folder webroot, pada turorial ini sesuai konfigurasi Nginx di atas, berada di /var/www/html/

chown -R nginx:nginx /var/www/html && cd /var/www/html && find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 644 {} \; && cd

Buka http://ip-server dengan browser

7. Tips

Gunakan perintah reload sebagai pengganti restart untuk meminimalisir web server down jika Anda memperbarui file konfigurasi Nginx dan PHP-FPM. Contoh,

service nginx reload
service php-fpm reload

Selamat mencoba 😀

4.5 of 5

from 2 ratings

Leave a Reply

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