How To Install LEMP Stack on Debian 8 Jessie (Nginx, MariaDB, PHP7)
LEMP stands for Linux, Nginx (Pronounced Egnine X), MySQL/MariaDB and PHP. It enables a server to host dynamic website and web apps. In this tutorial I will show you how to install LEMP stack on Debian 8 Jessie. I choose to install MariaDB as the database server instead of MySQL and install PHP7.
Step-1 Install Nginx
Nginx is a lightweight and fast web server compared to Apache and becomes more and more popular these days. It can also be used as a reverse proxy. To install Nginx on Debian 8, enter the following command in terminal:
$ sudo apt-get install nginx -y
Once installed, Nginx should be running.
$ sudo service nginx status [ ok ] nginx is running.
Check Nginx version
$ nginx -v nginx version: nginx/1.6.2
Type your server IP address in your web browser, if you see the following text, then Nginx is correctly installed.
You can check your public IP by using the following command
$ curl http://icanhazip.com
You can also use curl on your sever to fetch the HTTP header
user@www:~$ curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 25 Jan 2016 16:33:28 GMT
Content-Type: text/html
Content-Length: 867
Last-Modified: Mon, 25 Jan 2016 16:20:36 GMT
Connection: keep-alive
ETag: "56a64b54-363"
Accept-Ranges: bytes
As you can see, Nginx returns 200 response code indicating Nginx is running correctly.
Install MariaDB
MariaDB is a drop-in relpacement for MySQL. Install it using following commmand:
sudo apt-get install -y mariadb-server mariadb-client
During the installation process, it will ask you to set a password for Mariadb root user. Enter a good password and hit enter. MariaDB root user should not be confused with Debian system root user.
Repeat your password and hit enter.
Check MariaDB version
$ mysql --version mysql Ver 15.1 Distrib 10.0.22-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
Now run the security script
$ sudo mysql_secure_installation
Enter MariaDB root password. When it asks if you want to change root password, press n
key. Then you can simply press enter to answer all of other questions.
MariaDB installation is complete.
Install PHP7
Put the following text in /etc/apt/sources.list file to add dotdeb.org repository.
deb http://packages.dotdeb.org jessie all deb-src http://packages.dotdeb.org jessie all
Fetch and install the GnuPG key
$ wget https://www.dotdeb.org/dotdeb.gpg $ sudo apt-key add dotdeb.gpg
Update local package index and install PHP7.
$ sudo apt-get update $ sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl php7.0-xml php7.0-zip php7.0-mbstring
Configure PHP7
Edit the main php-fpm config file:
sudo vi /etc/php/7.0/fpm/php.ini
Find the following line:
;cgi.fix_pathinfo=1
remove the preceeding semicolon and set it’s value to 0
cgi.fix_pathinfo=0
Save and close the file. Restart php-fpm
sudo service php7.0-fpm restart
Configure Nginx Virtual Host
Create a new virtual host file under /etc/nginx/sites-available directory:
sudo vi /etc/nginx/sites-available/yourdomain.conf
Add the following lines to it.
server {
listen 80;
server_name yourdoman.com www.yourdomain.com;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
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$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Replace yourdoman.com with your actual domain name. Save and close the file. Create the soft link:
sudo ln -s /etc/nginx/sites-available/yourdomain.conf /etc/nginx/sites-enabled/yourdomain.conf
Then test Nginx configuration
user@www:$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx configuration
$ sudo service nginx reload
Give full permission of /var/www/html to www-data user.
sudo chown www-data:www-data /var/www/* -R
Test PHP
Create a info.php file in the web root directory:
$ sudo vi /var/www/html/info.php
Put the following text inot info.php file.
<?php phpinfo(); ?>
Save and close the file. Now in your browser’s address bar, type the following text:
yourdomain.com/info.php
Replace yourdomain.com with your actual domain name.
If you can see something like this, then your PHP is working correctly.
Make sure you have set a correct A record for your domain name.
The info.php file is for testing only. For security reassons you can now remove it. Now you have successfuly install LEMP stack (Nginx, Mariadb, PHP7-FPM) on Debian 8 Jessie.
Cheers!
hi there , thank you for an excellent guide .I had some problems when installed php7 , I ‘ve added “deb http://packages.dotdeb.org jessie all and deb – src http://packages.dotdeb.org jessie all” in the ( sudo nano / etc / apt / sources .list ) .
when I tried sudo apt- get update comes out error :
W: Failed to fetch http://packages.dotdeb.org/dists/jessie/InRelease Unable to find expected entry ‘all/binary-armhf/Packages’ in Release file (Wrong sources.list entry or malformed file)
E: Some index files failed to download. They have been ignored, or old ones used instead.
you have a solution?
hi,
great tuto.
Just one thing that i don’t understand when i do the same.
Instead of executing the script test in php, my browser show me the code
Any idea of what i’v done wrong ?
Thanks, great job
Simply remove the symbolic link in nginx/sites-enabled which is named default. Obviously otherwise nginx will not take the php settings fo your server config instead it uses the default one.
Regards,
Michael