How to Install Nginx, Mysql, PHP ( LEMP ) Server on Ubuntu 16.04

lemp-ubuntu


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-server-password-prompt-page-pic

Note: Confirm Password for Mysql root user.

password-reconfirmation-page-pic

Step 7. Install Php7 by running bellow command from your terminal.

# apt-get install php-fpm php-mysql  -y

Step 8. 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 9. Configure Nginx for php by modify the config file of nginx.

# vim /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.php 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;
}
  
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

# deny access to .htaccess files, if Apache’s document root

# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

Step 10. Restart Nginx.

# service nginx restart

Step 11. 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.

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

<?php
phpinfo();
?>

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

http://ip/info.php

 

php-7-0-page-pic