Cara install LEMP di Ubuntu 18.04

By | August 11, 2018

Panduan cara install Nginx, MariaDB, PHP atau LEMP stack di Linux Ubuntu 18.04 LTS. Selaku bonus, di panduan ini dilengkapi dengan instalasi Redis server dan OPCache bagi meningkatkan performa web server serta konfigurasi dan optimasi Nginx.

Diawali dengan update program Linux

apt update

Langkah 1 – Install MariaDB

Secara default di Linux 18.04 LTS paket sistem MariaDB telah menggunakan versi mutakhir, yaitu MariaDB versi 10. begitupun dengan PHP telah versi 7.2.

apt install mariadb-server -y

Kemudian tingkatkan kemanan instalasi MariaDB, seperti setup password bagi akun root, disable remote akses MySQL dan menghapus database yang tak terpakai, (test), jalankan petunjuk berikut

mysql_secure_installation

Jawab pertanyaan berikut ini dengan yang saya tandai warna merah, lalu tekan Enter.

Enter current password for root (enter for none): Tekan Enter
Set root password? [Y/n] y
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 dan modul

Baris petunjuk dibawah ini akan meng-install modul-modul urgen agar Nginx, MySQL dan PHP dapat berfungsi dan terintegrasi dengan bagus, diantaranya: php-fpm dan php-mysql.

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

Install OPCache dan Redis

Langkah ini optional, Kamu dapat melewatinya kalau sedang terburu-buru atau seketika ke langkah 3 –  install Nginx.

OPCache bermanfaat bagi meningkatkan performa PHP dengan cara menyimpan skrip precompiled bytecode pada shared memori, sehingga tak perlu mengandung dan mem-parsing skrip di setiap request.

apt install php-opcache

Menej php.ini

nano /etc/php.ini

Cari baris berikut, sesuaikan

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

Dilanjut dengan pemasangan redis-server dan ekstensi php-redis

Selain OPCache, Redis (REmote DIctionary Server) akan melengkapi kesempurnaan performa web server digunakan bagi caching, message broker dan database agar dapat mengurangi pemakaian bandwith dan server load, betul-betul cocok bagi situs yang sibuk.

apt install redis server  apt install php-redis -y

Aktifkan redis

systemctl start redis-server
systemctl enable redis-server.service

Setelah selesai proses instalasi, cek apakah Redis telah aktif

redis-cli

Ketik ping dan Enter, hasilnya harusnya seperti ini

127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

Pengguna WordPress, silahkan ikuti panduan cara install dan konfigurasi Redis.

Langkah 3 – Install Nginx

Di Linux Ubuntu 18.04 paket sistem Nginx web server versi terbaru telah ada di repository, jadi seketika saja

apt install nginx -y

Kemudian start service Nginx dan aktifkan dikala program booting

systemctl start nginx  systemctl enable nginx

Langkah 4 – Konfigurasi dan optimasi Nginx

Menej konfigurasi virtual host Nginx pakai nano teks editor.

echo > /etc/nginx/sites-available/default  nano /etc/nginx/sites-available/default

Kemudian copy-paste theme 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 telah dikompres oleh 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/php7.2-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;

}
}

Restart service php-fpm  dan nginx

systemctl restart php-fpm  systemctl restart nginx

Kemudian bikin file informasi.php bagi memeriksa apakah Nginx, PHP-FPM serta modul-modul PHP lain telah bekerja dengan bagus. Jalankan petunjuk berikut:

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

Ubah hak kepemilikan (chown) www-data dan chmod file (644) dan folder (755) yang terdapat di direktori web root /var/www/html

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

Akses menggunakan browser http://ip-server, pastikan kolom Server API yaitu FPM/FastCGI dan ada ekstensi Redis disana.

Langkah 5 – Troubleshooting

Sekiranya IP tak dapat diakses lewat browser, cek apakah firewall aktif, kalau iya tambahkan service http (buka port 80) dengan petunjuk berikut:

ufw allow http
ufw allow https

Selamat mencoba ?

Catatan:

Sebelum dipublikasikan panduan ini telah dites di Linux Ubuntu 18.04.2 LTS dan bekerja dengan betul-betul bagus. Di panduan berikutnya akan dibahas cara install LEMP di CentOS 7.

Referensi:


Sumber https://idnetter.com