Installing LEMP Server (Nginx,MariaDB,PHP) On Ubuntu 14.04
From Zam Wiki
Contents
Introduction
LEMP is a combination of the operating system and open-source software stack. The acronym LEMP comes from the first letters of Linux, Nginx(engine-x) web server, MariaDB database, and PHP.
In this tutorial, let us see how to install Nginx, MariaDB, PHP on Ubuntu 14.04.
Install Nginx
Nginx (pronounced as engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.
If you have already installed apache2 in your system, remove it first to avoid conflicts. To uninstall apache, run the following commands:
sudo apt-get purge apache2* sudo apt-get autoremove -y
Now, install Nginx using command:
sudo apt-get install nginx
Start Nginx service using the command:
sudo service nginx start
Test Nginx
Open up your web browser and navigate to http://server-ip-address/ or http://127.0.0.1/. You will see a screen something like below.
Configure Nginx
Check how many no. of CPU’s that your server have. Use command lscpu. In my case it’s 4. So I set this to 4.
root@test-server:~# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4
Open the file /etc/nginx/nginx.conf in any editor:
sudo nano /etc/nginx/nginx.conf
Set the worker_processes (bases on your no. of CPU’s in your server):
worker_processes 4;
Restart Nginx service:
sudo service nginx restart
The default vhost(server block) is defined in the /etc/nginx/sites-available/default file. Open the file /etc/nginx/sites-available/default in any editor.
sudo nano /etc/nginx/sites-available/default
Delete all line in that file. Then replace it with this line below:
server { listen 80; root /usr/share/nginx/html; index index.php index.htm index.html; server_name testserver.com; fastcgi_buffers 8 16k; fastcgi_buffer_size 32k; fastcgi_read_timeout 180; location / { try_files $uri $uri/ /index.html; } location ~ \.php$ { try_files $uri $uri/ =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } }
- Replace testserver.com with your full domain name or your server IP address.
Save and exit the file (press CTRL-O to save and CTRL-X to exit).
Test Nginx configuration
Test the nginx configuration for any syntax errors using command:
sudo nginx -t
Sample output:
root@test-server:~# sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Finally restart nginx service
sudo service nginx restart
Install MariaDB
MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.
First you have to remove existing MySQL packages if any. To completely uninstall MySQL with configuration files, enter the following command:
sudo apt-get remove mysql* sudo apt-get autoremove -y
After removing MySQL, run the following command to install MariaDB:
sudo apt-get install mariadb-server mariadb-client -y
During installation you will be asked to set database root user password:
Re-enter password:
Click Yes to migrate to MariaDB.
- Note that you'll not be asked this question if you install MariaDB before MySQL.
Check MariaDB version
You can check the MariaDB version using command:
sudo mysql -v -u root -p
Sample output:
root@test-server:~# sudo mysql -v -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 5896 Server version: 5.5.41-MariaDB-1ubuntu0.14.04.1 (Ubuntu) Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others. Reading history-file /root/.mysql_history Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
Then type 'exit to exit mysql console.
Check MariaDB status
To check if MariaDB is running or not, using the following command:
sudo service mysql status
Sample output:
root@test-server:~# sudo service mysql status * /usr/bin/mysqladmin Ver 9.0 Distrib 5.5.41-MariaDB, for debian-linux-gnu on x86_64 Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others. Server version 5.5.41-MariaDB-1ubuntu0.14.04.1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 8 days 8 hours 36 min 19 sec Threads: 5 Questions: 1387687 Slow queries: 0 Opens: 97 Flush tables: 2 Open tables: 56 Queries per second avg: 1.921
Install PHP
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.
Install PHP with following command:
sudo apt-get install php5 php5-fpm php5-mysql
Configure PHP-FPM
PHP-FPM listens on the socket /var/run/php5-fpm.sock by default. If you want to make PHP-FPM use a TCP connection, open the file /etc/php5/fpm/pool.d/www.conf:
sudo nano /etc/php5/fpm/pool.d/www.conf
Find the line listen = /var/run/php5-fpm.sock:
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = /var/run/php5-fpm.sock
- You can use command CTRL-W to search that line.
Modify it to listen = /tmp/php5-fpm.sock.
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = /var/run/php5-fpm.sock listen = /tmp/php5-fpm.sock
- Add this listen = /tmp/php5-fpm.sock line at the end of FastCGI segment.
Save and exit the file (press CTRL-O to save and CTRL-X to exit).
Restart php5-fpm service.
sudo service php5-fpm restart
Your php5-fpm.sock in /tmp/ folder should have this kind of permission and ownership:
root@test-server:~# ls -alh /tmp/ total 8.0K drwxrwxrwt 2 root root 4.0K Apr 4 00:30 . drwxr-xr-x 22 root root 4.0K Mar 26 15:32 .. srw-rw---- 1 www-data www-data 0 Mar 26 15:32 php5-fpm.sock
If not, you need to change it accordingly.
Test PHP
Create a sample testphp.php file in nginx document root folder:
cd /usr/share/nginx/html/ sudo nano testphp.php
Add the following lines in it.
<?php phpinfo(); ?>
Save and exit the file (press CTRL-O to save and CTRL-X to exit).
Test PHP via browser
Navigate to http://server-ip-address/testphp.php or http://127.0.0.1/. It will display all the details about PHP such as version, build date and commands etc.
- Credit to http://www.unixmen.com/install-lemp-server-nginx-mysql-mariadb-php-phpmyadmin-ubuntu-14-1014-0413-10 for the original tutorial.