Cara install LEMP di CentOS 7

By | August 11, 2018

Panduan komplit cara install LEMP stack Nginx, MariaDB 10 dan PHP 7.2 di Linux CentOS 7 dengan paket-paket sistem aplikasi terbaru serta optimasi Nginx web server. Di panduan ini saya sertakan bagaimana cara install Redis dan module PHP OPCache.

Diawali dengan menambahkan epel repo dan update system Linux

yum install epel-release
yum update -y

Catatan : sebelum dipubliksikan, panduan ini telah dites di Linux CentOS 7.2  dan berjalan dengan sempurna, jadi diinginkan teman-teman tak menjumpai kendala mengikuti strategi berikut.

langkah 1 – Install MariaDB

MariaDB digadang-gadang performanya lebih bagus ketimbang MySQL, tapi sayangnya di CentOS 7.2 versi MariaDB defaultnya masih 5.x, Jadi kita perlu menambahkan repositori Mariadb versi 10.

Bikin file repo bagi MariaDB 10, menggunakan nano

nano /etc/yum.repos.d/MariaDB.repo

copy-paste teks berikut

[mariadb]
name = MariaDB
baseurl = https://yum.mariadb.org/10.2/centos7-amd64/
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Simpan, kemudian update

yum update

Baru jalankan command install mariadb

yum install mariadb-server -y

Setelah berhasil start dan aktifkan service mariadb

systemctl start mariadb.service
systemctl enable mariadb.service

Kemudian bagi meningkatkan keamanan instalasi MariaDB, dilanjut dengan menjalankan command berikut.

mysql_secure_installation

Jawablah pertanyaan-pertanyaan yang terdapat, berikut ini saya tandai dengan warna merah

Enter current password for root (enter for none): Tekan Enter
Set root password? [Y/n] y (kemudian ketik password yang diinginkan)
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y

Langkah 2 – Install PHP

Versi PHP di Centos 7 aslinya masih 5.4.16, di panduan ini kita menggantinya dengan menginstall versi 7.2 dengan menambahkan repo remi-php72.

yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Berikutnya install yum-utils dan enable remi-php72

yum install yum-utils -y  yum-config-manager --enable remi-php72

Jalankan command update

yum update

Baru setelahnya install PHP

yum -y install php-fpm php-cli php-mysql php-gd php-ldap php-odbc php-pdo php-pecl-memcache php-pear php-mbstring php-xml php-xmlrpc php-mbstring php-snmp php-soap

Jikalau butuh modul PHP lainnya, Sahabat dapat melihat ketersediaan modul dengan command yum search php-

Menej konfigurasi php.ini (saya sering lupa di bagian ini, dapat jadi Sahabat pun, jadi setelah instalasi PHP selesai seketika saja modifikasi php.ini)

nano /etc/php.ini

Cari baris berikut, sesuaikan

cgi.fix_pathinfo=0
upload_max_filesize = 32M
post_max_size = 32M
max_execution_time = 90

Kita pun membutuhkan module bagi caching PHP script. Selaku contoh saya memilih OPcache, OPcache meningkatkan performa PHP dengan menyimpan skrip precompiled bytecode pada shared memori, sehingga PHP tak perlu mengandung dan mem-parsing skrip di setiap request.

yum install php-opcache

Aktifkan php-fpm

systemctl start php-fpm
systemctl enable php-fpm.service

Install Redis

Sahabat dapat melewati langkah ini bila tak ingin memakai Redis, seketika ke langkah 3

yum install redis
systemctl start redis
systemctl enable redis.service

Coba cek redis, jalankan command berikut

redis-cli

Ketik ping dan Enter, outputnya seperti dibawah ini

127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

Berikutnya, bila situs Sahabat ber-platform WordPress, Redis menjadi alternatif yang bagus selaku aplikasi cache bagi WordPress, jadi:

yum install php-pecl-redis

Jangan lupa untu menginstall plugin Redis Object Cache, atau ikuti panduan cara install dan konfigurasi Redis

Langkah 3 – Install Nginx

Seketika saja jalankan satu baris command berikut bagi meng-install Nginx, start dan mengaktifkan Nginx dikala booting.

yum install nginx -y  systemctl start nginx  systemctl enable nginx

Bikin Nginx memakai unix socket php-fpm, modifikasi konfigurasi php-fpm

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

Ubah value di baris-baris berikut

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

Menej konfigurasi virtual host default Nginx

nano /etc/nginx/conf.d/default.conf

Hapus seluruh bila ada konten didalamnya, ganti dengan teks berikut

server {
listen 80;
listen [::]:80;
server_name _;

location /error/ {
alias /usr/share/nginx/html/errors/;
}

root /var/www/html/;
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; }

# Compress
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;

gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon image/png image/gif image/jpeg text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/xml
text/vtt text/x-component text/x-cross-domain-policy;
# text/html is always compressed by gzip module

# 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 file index.php di /var/www/html/

echo <?php phpinfo();  | sudo tee /var/www/html/index.php

Ubah owner dan group (nginx:nginx) dan chmod tiap folder (755) dan file (644)

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

Kemudian restart nginx dan php-fpm

systemctl restart nginx  systemctl restart php-fpm

Cek menggunakan browser, akses http://IP. Sahabat dapat mengetahui IP server kamu dengan command hostname -I

Langkah 4 – Troubleshooting

Jikalau IP server tak dapat dibuka via browser, kemungkinan Firewall aktif, cek dengan command berikut

firewall-cmd --state

Jikalau hasinya

running

Artinya Firewall aktif, izinkan service HTTP dan HTTPS

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

Selamat mencoba.


Sumber https://idnetter.com