Cara Menginstal LEMP Stack Pada Ubuntu


Lemp stack merupakan sekumpulan paket aplikasi yang digunakan untuk membangun web server. LEMP sendiri merupakan kepanjangan dari Linux, Nginx (dibaca Engine X), MySQL/MariaDB, PHP/Perl/Python. Perbedaannya dengan LAMP stack adalah pada web servernya, dimana lemp menggunakan nginx sedangkan lamp menggunakan apache.

Linux-Nginx-MariaDB-PHP
sumber gambar : www.digitalocean.com

Pada postingan ini akan dibahas mengenai cara menginstal lemp stack pada sistem operasi Ubuntu 16.04. Paket-paket yang diperlukan antara lain :

  • Nginx
  • MariaDB/MySQL (server)
  • PHP-FPM

Instalasi MySQL/MariaDB

Di sini saya menggunakan mariadb sebagai server databasenya. Meski begitu kalian juga bisa menggunakan mysql karena cara instalasinya tidak berbeda.

Install mariadb server.
xenial@ubuntu:~$ sudo apt install mariadb-server

Jika ingin mengintsal mysql :
xenial@ubuntu:~$ sudo apt install mysql-server

Pada saat proses instalasi nanti kalian akan diminta mengkonfigurasi password untuk akses database.

Setelah instalasi selesai, lakukan konfigurasi pengamanan database.
xenial@ubuntu:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Instalasi Nginx

Instal web server nginx.
xenial@ubuntu:~$ sudo apt install nginx

Cek apakah service nginx telah berjalan
xenial@ubuntu:~$ sudo service nginx status

Instalasi PHP-FPM

Berbeda dengan apache yang menggunakan 'php' saja. Pada web server nginx, paket php yang digunakan adalah php-fpm.

Instal php-fpm.
xenial@ubuntu:~$ sudo apt install php-fpm php-mysql
Pada tutorial ini, php yang digunakan adalah php versi terbaru yakni php7. Jika kalian ingin menggunakan php versi 5 maka kalian bisa melihat postingan cara menginstal php5 pada ubuntu.

Konfigurasi Site Nginx

Meskipun paket php-fpm sudah diinstal, namun web server nginx belum bisa menjalankan sekrip php. Untuk itu kita perlu mengedit konfigurasi site default yang terletak di direktori /etc/nginx/sites-available.
xenial@ubuntu:~$ sudo nano /etc/nginx/sites-available/default

Lakukan perubahan pada beberapa bagian berikut,
      # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
               deny all;
        }
}

Simpan file konfigurasi dan kemudian restart service nginx.
xenial@ubuntu:~$ sudo systemctl restart nginx

Jangan lupa mengecek status service nginx dan php-fpm.
xenial@ubuntu:~$ sudo systemctl status nginx
xenial@ubuntu:~$ sudo systemctl status php7.0-fpm

Membuat File PHP

Langkah terakhir adalah membuat file php pada direktori /var/www/html.
root@ubuntu:~# echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Perintah di atas akan membuat sebuah file php dengan nama info.

Lakukan pengecekan dengan mengakses url http://ip-server/info.php
Apabila sudah muncul tampilan seperti di atas maka php sudah berjalan dan kita telah berhasil menginstal paket LEMP stack.

Load Comments