How to Install a LEMP (Linux, Nginx, MySQL, PHP) Stack on Ubuntu 16.04
Introduction
The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. This is an acronym that describes a Linux operating system, with an Nginx web server. The backend data is stored in the MySQL database and the dynamic processing is handled by PHP.
In this guide, we will demonstrate how to install a LEMP stack on an Ubuntu 16.04 server. The Ubuntu operating system takes care of the first requirement. We will describe how to get the rest of the components up and running.
Step 1: Install the Nginx/1.10.3 Web Server
1. Nginx is a modern and resources efficient web server used to display web pages to visitors on the internet. We’ll start by installing Nginx web server from Ubuntu official repositories by using the apt command line.
1 |
sudo apt-get install nginx |
2. Next, issue the netstat and systemctl commands in order to confirm if Nginx is started and binds on port 80.
1 |
netstat -tlpn |
Result
1 2 3 4 5 6 7 8 9 10 |
test@nginx:/$ netstat -tlpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 156/rpcbind tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 803/nginx -g daemon tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 202/sshd tcp6 0 0 :::111 :::* LISTEN 156/rpcbind tcp6 0 0 :::80 :::* LISTEN 803/nginx -g daemon tcp6 0 0 :::22 :::* LISTEN 202/sshd test@nginx:/$ |
1 |
sudo systemctl status nginx.service |
Result
1 2 3 4 5 6 7 8 9 10 11 12 13 |
test@nginx:/$ sudo systemctl status nginx.service ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2017-12-12 15:28:37 WIB; 31min ago Main PID: 803 (nginx) CGroup: /system.slice/nginx.service ├─803 nginx: master process /usr/sbin/nginx -g daemon on; master_process on ├─804 nginx: worker process └─805 nginx: worker process Dec 12 15:28:36 nginx systemd[1]: Starting A high performance web server and a reverse proxy server... Dec 12 15:28:37 nginx systemd[1]: Started A high performance web server and a reverse proxy server. test@nginx:/$ |
Once you have the confirmation that the server is started you can open a browser and navigate to your server IP address or DNS record using HTTP protocol in order to visit Nginx default web page.
If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by typing one of the following into your terminal:
1 |
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' |
This will print out a few IP addresses. You can try each of them in turn in your web browser.
As an alternative, you can check which IP address is accessible as viewed from other locations on the internet:
1 |
curl -4 icanhazip.com |
Type one of the addresses that you receive in your web browser. It should take you to Nginx’s default landing page:
1 |
http://server_domain_or_IP |
If you see the above page, you have successfully installed Nginx.
Step 2: Install MySQL/5.7.20 to Manage Site Data
Now that we have a web server, we need to install MySQL, a database management system, to store and manage the data for our site.
You can install this easily by typing:
1 |
sudo apt-get install mysql-server |
You will be asked to supply a root (administrative) password for use within the MySQL system.
After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.
1 |
sudo systemctl status mysql |
The MySQL database software is now installed, but its configuration is not exactly complete yet.
To secure the installation, we can run a simple security script that will ask whether we want to modify some insecure defaults. Begin the script by typing:
1 |
sudo mysql_secure_installation |
Step 3: Install PHP/7.0.25 for Processing
Nginx can be used with PHP dynamic processing language interpreter to generate dynamic web content with the help of FastCGI process manager obtained by installing the php-fpm binary package from Ubuntu official repositories.
Enter the following command to install PHP7 and PHP7 extensions.
1 |
sudo apt-get install mcrypt php7.0-cli php7.0-cgi php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext |
Configure the PHP Processor
We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure.
Open the main php-fpm configuration file with root privileges:
1 |
sudo nano /etc/php/7.0/fpm/php.ini |
Find the line ‘cgi.fix_pathinfo=1
′, uncomment it and change the value 1 to 0.
1 |
cgi.fix_pathinfo=0 |
fix warning from date() in PHP
1 |
date.timezone = Asia/Jakarta |
Save and close the file when you are finished.
Now, we just need to restart our PHP processor by typing:
1 |
sudo systemctl restart php7.0-fpm |
This will implement the change that we made.
Step 4: Configure Nginx to Use the PHP Processor
Now, we have all of the required components installed. The only configuration change we still need is to tell Nginx to use our PHP processor for dynamic content.
The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here: http://wiki.nginx.org/NginxFullExample2)
We do this on the server block level (server blocks are similar to Apache’s virtual hosts). Open the default Nginx server block configuration file by typing:
1 |
sudo nano /etc/nginx/sites-available/default |
Paste the following text into the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
## # Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { include snippets/fastcgi-php.conf; include fastcgi_params; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /\.ht { deny all; } } |
Save the changes and test the new configuration
1 |
sudo nginx -t |
If any errors are reported, go back and recheck your file before continuing.
When you are ready, reload Nginx to make the necessary changes:
1 |
sudo systemctl reload nginx |
Step 5: Create a PHP File to Test Configuration
Your LEMP stack should now be completely set up. We can test it to validate that Nginx can correctly hand .php
files off to our PHP processor.
We can do this by creating a test PHP file in our document root. Open a new file called info.php
within your document root in your text editor:
1 |
sudo nano /var/www/html/info.php |
Type or paste the following lines into the new file. This is valid PHP code that will return information about our server:
1 2 3 |
<?php phpinfo(); ?> |
When you are finished, save and close the file.
Now, you can visit this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php
:
1 |
http://server_domain_or_IP/info.php |
You should see a web page that has been generated by PHP with information about your server:
If you see a page that looks like this, you’ve set up PHP processing with Nginx successfully.
After verifying that Nginx renders the page correctly, it’s best to remove the file you created as it can actually give unauthorized users some hints about your configuration that may help them try to break in. You can always regenerate this file if you need it later.
That’s it. Your LEMP server is ready to use.
Cheers!
Installing PhpMyAdmin on Ubuntu 16.04 LEMP
SOURCE