Install LEMP Stack

On Ubuntu 16.04:
#1. Install Nginx:
Install nginx using command:

$ sudo apt install nginx

Start Nginx service using the following command.

$ sudo systemctl start nginx

To check whether nginx service is started, run:

$ sudo systemctl status nginx

#2. Configure Nginx:

$ sudo vim /etc/nginx/nginx.conf
worker_processes x;

Restart Nginx service:
$ sudo systemctl restart nginx</code>

Open the file /etc/nginx/sites-available/default in any editor.

$ sudo vim /etc/nginx/sites-available/default
server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;
	root /usr/share/nginx/html;
	index index.php index.html index.htm;
	# Make site accessible from server_name
	server_name server_domain_name_or_IP;
	location / {
		try_files $uri $uri/ =404;
	error_page 404 /404.html;
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/share/nginx/html;
	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
// test nginx
$ sudo nginx -t
restart nginx service

#3. Install MySQL:

$ sudo apt install mysql-server mysql-client

You can verify the MySQL server status using command:

$ sudo systemctl status mysql
$ sudo mysql_secure_installation

#4. Install PHP:
PHP 5:

$ sudo apt install php php-fpm php-mysql php-curl php-gd php-mbstring php-gettext php-mcrypt php-xmlrpc

PHP 7: (replace php7 vs current version of php, in my case is 7.0 (sudo apt-cache search php7))

$ sudo apt install php7.0 php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-mbstring php-gettext php7.0-mcrypt php7.0-xmlrpc

#5. Configure PHP:
PHP 5:

$ sudo vim /etc/php/5.x/fpm/php.ini

PHP 7:

$ sudo vim /etc/php/7.x/fpm/php.ini

Find the line 'cgi.fix_pathinfo=1', uncomment it and change the value 1 to 0.


Now restart php-fpm service.
PHP 5:

$ sudo systemctl restart php5-fpm

PHP 7:

$ sudo systemctl restart php7.0-fpm

To check whether php5-fpm is running or not using command:

$ sudo systemctl status php5-fpm

To check whether php7-fpm is running or not using command:

$ sudo systemctl status php7.0-fpm

change mod for www:

find /var/www -type d -exec chmod 755 {} \;
find /var/www -type f -exec chmod 644 {} \;

#6. Install phpMyAdmin:

$ sudo apt install phpmyadmin

By default, nginx will not be displayed here.
So, select apache or lighttpd, and we will configure phpmyadmin to work with nginx webserver later.

$ sudo ln -s /usr/share/phpmyadmin/ /var/www/html


$ sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/html

Restart nginx server.

  • linux/lemp.txt
  • Last modified: 2017/07/17 13:41
  • by