How to Install Nginx, Mysql, PHP ( LEMP ) on Debian 8

debian-lemp

LEMP is a complete package to run any web application on a server. LEMP signifies the Linux, Nginx, MySQL and Php. Nginx is used as the web server for hosting of an application whereas MySQL is used as systematic data storage of application & Php is popular server-side scripting language which is used for web development as well bridging the gap between application and the database.


Step 1.
Open your EC2 Terminal and update your Packages.

# apt-get update

Step 2. Install nginx webserver.

# apt-get install nginx -y

Step 3. Now direct your browser to http://ip  and you should see the Nginx  page like this.

nginx-main-page-pic

Step 4. Install mysql-server in your Machine by running bellow command from your terminal.

# apt-get install mysql-server -y

Step 5. Specify New password for Mysql root user.

mysql-passwd-pic

Note: Confirm Password for Mysql root user.

mysql-passwd-confirm-pic

Step 7.  Execute below lines to get PHP Repository in your apt source file.

# echo deb http://packages.dotdeb.org jessie all >> /etc/apt/sources.list
# wget http://www.dotdeb.org/dotdeb.gpg
# apt-key add dotdeb.gpg

Step 8. Update Packages.

# apt-get update

Step 9. Install Php 7 by running bellow command from your terminal.

# apt-get install php7.0-fpm php7.0-mysql  -y

Step 10. Configure PHP by modify the config file of php. Make sure to uncomment & change the ;cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0.

# vim /etc/php/7.0/fpm/php.ini

cgi.fix_pathinfo=0

Step 11. Configure Nginx for php by modify the config file of nginx.

# vi /etc/nginx/sites-available/default

server {
listen 80 default_server;
listen [::]:80 default_server;
  
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don’t use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;

  
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
 
server_name YourServerIP or DomainName;

  
location / {

# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules;
}
  
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#
location ~ \.php$ {
include fastcgi_params;#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

Step 12. Restart Nginx.

# service nginx restart

Step 13. The document root of the webserver is /var/www/html. Now We will create  PHP file (info.php) in  /var/www/html/ directory and call it through a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

# vi /var/www/html/info.php

<?php
phpinfo();
?>

Step 14. Finish up by visiting your php info page.The page should look like this.

http://ip/info.php

php-page-pic