Install Nginx, MySQL, PHP7 as a Web Server on Ubuntu Server 16.04

Nginx merupakan sebuah server http atau webserver dan dapat berfungsi juga sebagai reverse proxy, Nginx dibaca Engine X. Nginx sendiri merupakan opensource dan gratis

Dalam tutorial kali ini kita akan membangun web server menggunakan Nginx, MySQL untuk Database, dan PHP (LEMP)

#Install Nginx

root@ubuntu:~# apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libgd3 libvpx3 libxpm4 libxslt1.1 nginx-common nginx-core
Suggested packages:
libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
libgd3 libvpx3 libxpm4 libxslt1.1 nginx nginx-common nginx-core
0 upgraded, 7 newly installed, 0 to remove and 73 not upgraded.
Need to get 1,492 kB of archives.
After this operation, 4,831 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
(Reading database ... 55940 files and directories currently installed.)
Removing apache2-bin (2.4.18-2ubuntu3.1) ...
dpkg: warning: while removing apache2-bin, directory '/var/lib/apache2' not empty so not removed
Removing apache2-data (2.4.18-2ubuntu3.1) ...
Removing apache2-utils (2.4.18-2ubuntu3.1) ...
Removing mysql-server-5.7 (5.7.16-0ubuntu0.16.04.1) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Removing mysql-server-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Removing mysql-client-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Removing mysql-client-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Removing libaio1:amd64 (0.3.110-2) ...
Removing libaprutil1-dbd-sqlite3:amd64 (1.5.4-1build1) ...
Removing libaprutil1-ldap:amd64 (1.5.4-1build1) ...
Removing libaprutil1:amd64 (1.5.4-1build1) ...
Removing libapr1:amd64 (1.5.2-3) ...
Removing libcgi-fast-perl (1:2.10-1) ...
Removing libhtml-template-perl (2.95-2) ...
Removing libcgi-pm-perl (4.26-1) ...
Removing libhttp-message-perl (6.11-1) ...
Removing libencode-locale-perl (1.05-1) ...
Removing libevent-core-2.0-5:amd64 (2.0.21-stable-2) ...
Removing libfcgi-perl (0.77-1build1) ...
Removing libhtml-parser-perl (3.72-1) ...
Removing libhtml-tagset-perl (3.20-2) ...
Removing libhttp-date-perl (6.02-1) ...
Removing libio-html-perl (1.001-1) ...
Removing liblua5.1-0:amd64 (5.1.5-8ubuntu1) ...
Removing liblwp-mediatypes-perl (6.02-1) ...
Removing libtimedate-perl (2.3000-2) ...
Removing liburi-perl (1.71-1) ...
Removing mysql-common (5.7.16-0ubuntu0.16.04.1) ...
Removing php7.0-cli (7.0.8-0ubuntu0.16.04.3) ...
Removing php7.0-readline (7.0.8-0ubuntu0.16.04.3) ...
Removing php7.0-opcache (7.0.8-0ubuntu0.16.04.3) ...
Removing php7.0-json (7.0.8-0ubuntu0.16.04.3) ...
Removing php7.0-common (7.0.8-0ubuntu0.16.04.3) ...
Removing ssl-cert (1.0.37) ...
Removing php-common (1:35ubuntu6) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

Sekarang coba akses IP server, disin IP server saya adalah 192.168.130.128

nginx

Nginx sudah ter-install, selanjutnya kita akan melakukan installasi MySQL server

#Install MySQL Server

root@ubuntu:~# apt-get -y install mysql-server mysql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl
libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl
libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client mysql-client-5.7 mysql-client-core-5.7
mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 22 newly installed, 0 to remove and 73 not upgraded.
Need to get 10.0 kB/18.8 MB of archives.
After this operation, 162 MB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client all 5.7.16-0ubuntu0.16.04.1 [10.0 kB]
Fetched 10.0 kB in 0s (15.6 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libevent-core-2.0-5:amd64.
(Reading database ... 54928 files and directories currently installed.)
Preparing to unpack .../libevent-core-2.0-5_2.0.21-stable-2_amd64.deb ...
Unpacking libevent-core-2.0-5:amd64 (2.0.21-stable-2) ...
Selecting previously unselected package mysql-common.
Preparing to unpack .../mysql-common_5.7.16-0ubuntu0.16.04.1_all.deb ...
Unpacking mysql-common (5.7.16-0ubuntu0.16.04.1) ...
Selecting previously unselected package libaio1:amd64.
Preparing to unpack .../libaio1_0.3.110-2_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-2) ...
Selecting previously unselected package mysql-client-core-5.7.
Preparing to unpack .../mysql-client-core-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../mysql-client-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb ...
Unpacking mysql-client-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Selecting previously unselected package mysql-server-core-5.7.
Preparing to unpack .../mysql-server-core-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb ...
Unpacking mysql-server-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mysql-common (5.7.16-0ubuntu0.16.04.1) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Selecting previously unselected package mysql-server-5.7.
(Reading database ... 55092 files and directories currently installed.)
Preparing to unpack .../mysql-server-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb ...
Unpacking mysql-server-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Selecting previously unselected package libhtml-tagset-perl.
Preparing to unpack .../libhtml-tagset-perl_3.20-2_all.deb ...
Unpacking libhtml-tagset-perl (3.20-2) ...
Selecting previously unselected package liburi-perl.
Preparing to unpack .../liburi-perl_1.71-1_all.deb ...
Unpacking liburi-perl (1.71-1) ...
Selecting previously unselected package libhtml-parser-perl.
Preparing to unpack .../libhtml-parser-perl_3.72-1_amd64.deb ...
Unpacking libhtml-parser-perl (3.72-1) ...
Selecting previously unselected package libcgi-pm-perl.
Preparing to unpack .../libcgi-pm-perl_4.26-1_all.deb ...
Unpacking libcgi-pm-perl (4.26-1) ...
Selecting previously unselected package libfcgi-perl.
Preparing to unpack .../libfcgi-perl_0.77-1build1_amd64.deb ...
Unpacking libfcgi-perl (0.77-1build1) ...
Selecting previously unselected package libcgi-fast-perl.
Preparing to unpack .../libcgi-fast-perl_1%3a2.10-1_all.deb ...
Unpacking libcgi-fast-perl (1:2.10-1) ...
Selecting previously unselected package libencode-locale-perl.
Preparing to unpack .../libencode-locale-perl_1.05-1_all.deb ...
Unpacking libencode-locale-perl (1.05-1) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../libhtml-template-perl_2.95-2_all.deb ...
Unpacking libhtml-template-perl (2.95-2) ...
Selecting previously unselected package libtimedate-perl.
Preparing to unpack .../libtimedate-perl_2.3000-2_all.deb ...
Unpacking libtimedate-perl (2.3000-2) ...
Selecting previously unselected package libhttp-date-perl.
Preparing to unpack .../libhttp-date-perl_6.02-1_all.deb ...
Unpacking libhttp-date-perl (6.02-1) ...
Selecting previously unselected package libio-html-perl.
Preparing to unpack .../libio-html-perl_1.001-1_all.deb ...
Unpacking libio-html-perl (1.001-1) ...
Selecting previously unselected package liblwp-mediatypes-perl.
Preparing to unpack .../liblwp-mediatypes-perl_6.02-1_all.deb ...
Unpacking liblwp-mediatypes-perl (6.02-1) ...
Selecting previously unselected package libhttp-message-perl.
Preparing to unpack .../libhttp-message-perl_6.11-1_all.deb ...
Unpacking libhttp-message-perl (6.11-1) ...
Selecting previously unselected package mysql-client.
Preparing to unpack .../mysql-client_5.7.16-0ubuntu0.16.04.1_all.deb ...
Unpacking mysql-client (5.7.16-0ubuntu0.16.04.1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_5.7.16-0ubuntu0.16.04.1_all.deb ...
Unpacking mysql-server (5.7.16-0ubuntu0.16.04.1) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libevent-core-2.0-5:amd64 (2.0.21-stable-2) ...
Setting up libaio1:amd64 (0.3.110-2) ...
Setting up mysql-client-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Setting up mysql-client-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Setting up mysql-server-core-5.7 (5.7.16-0ubuntu0.16.04.1) ...
Setting up mysql-server-5.7 (5.7.16-0ubuntu0.16.04.1) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv OK
mysql.db OK
mysql.engine_cost OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.gtid_executed OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.innodb_index_stats OK
mysql.innodb_table_stats OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.proxies_priv OK
mysql.server_cost OK
mysql.servers OK
mysql.slave_master_info OK
mysql.slave_relay_log_info OK
mysql.slave_worker_info OK
mysql.slow_log OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
The sys schema is already up to date (version 1.5.1).
Checking databases.
sys.sys_config OK
Upgrade process completed successfully.
Checking if update is needed.
Setting up libhtml-tagset-perl (3.20-2) ...
Setting up liburi-perl (1.71-1) ...
Setting up libhtml-parser-perl (3.72-1) ...
Setting up libcgi-pm-perl (4.26-1) ...
Setting up libfcgi-perl (0.77-1build1) ...
Setting up libcgi-fast-perl (1:2.10-1) ...
Setting up libencode-locale-perl (1.05-1) ...
Setting up libhtml-template-perl (2.95-2) ...
Setting up libtimedate-perl (2.3000-2) ...
Setting up libhttp-date-perl (6.02-1) ...
Setting up libio-html-perl (1.001-1) ...
Setting up liblwp-mediatypes-perl (6.02-1) ...
Setting up libhttp-message-perl (6.11-1) ...
Setting up mysql-client (5.7.16-0ubuntu0.16.04.1) ...
Setting up mysql-server (5.7.16-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...

Untuk memastikan MySQL sudah terinstall dengan benar ketikan perintah berikut:

root@ubuntu:~# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Apabila anda telah berhasil masuk kedalam console mysql> berarti MySQL sudah terinstall, secara default port MySQL berjalan pada 3306

#Install PHP7

root@ubuntu:~# apt-get install  -y php7.0-mysql php7.0-curl php7.0-json

Sekarang PHP7 sudah terinstall, coba buat 1 file untuk test PHP

echo "<?php phpinfo(); ?>" > /var/www/html/php.php

Setelah file tersebut dibuat, silahkan akses IP server http://<ip>/php.php

Benar file php tersebut malah di download oleh browser, ini dikarenakan konfigurasi PHP dan Nginx belum diatur,

#Configuration Nginx > /etc/nginx/site-available/default
#Configurasi ini berlaku untuk setiap virtual_host(vhost) yang akan anda buat, semisalnya domainkamu.com, cukup tambahkan nama doamin atau alias sesudah server_name Contoh : server_name domainkamu.com
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
#server_name domainkamu.com #contoh
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
}
}

Sekarang nginx untuk defaultnya sudah dikonfigurasikan, namun anda juga harus melakukan konfigurasi pada PHP7, silahkan edit seperti berikut

#Configuration file > /etc/php/7.0/fpm/pool.d/www.conf

[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

 

Restart services nginx dan php7.0-fpm

root@ubuntu:~# /etc/init.d/nginx restart
[ ok ] Restarting nginx (via systemctl): nginx.service.
root@ubuntu:~# /etc/init.d/php7.0-fpm restart
[ ok ] Restarting php7.0-fpm (via systemctl): php7.0-fpm.service.

Sekarang coba akses lagi IP server http://<ip>/php.php

Jika sudah berhasil akan tampak php info seperti berikut

phpinfo

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

*