Tutorials

How to Install LEMP Stack on Ubuntu 17.10

Install LEMP Stack on ubuntu

Insight: Install LEMP Stack on Ubuntu 17.10

We will show you the process to install LEMP stack or Nginx, MariaDB, PHP7.1 on ubuntu opearing system.

How to Install LEMP Stack on Ubuntu 17.10

Update your ubuntu along with repository and software packages.

sudo apt update

sudo apt upgrade

Now, Install Nginx Web Server

Install LEMP Stack on ubuntu

Open your terminal and type the following command.

sudo apt install nginx

Enable ngink with the command below:

sudo systemctl enable nginx

Now start Nginx:

sudo systemctl start nginx

Now check the status of nginx server.

systemctl status nginx

If this installation is being done on local Ubuntu 17.10 computer, then type  127.0.0.1 or localhost in the browser address bar and you will see Welcome page.

Note: As web root directory is owned by the root user, we need to make Nginx user or www-data as the owner of the root.

sudo chown www-data:www-data /usr/share/nginx/html -R

Method to install MariaDB database server

Install LEMP Stack on ubuntu

Command for installation.

sudo apt install mariadb-server mariadb-client

Use systemctl to check status.

systemctl status mariadb

MariaDB should automatically started. If it’s not running, start it with this command:

sudo systemctl start mariadb

Make it start at boot time.

sudo systemctl enable mariadb

Install PHP7.1

Install LEMP Stack on ubuntu

PHP7.1 is included in Ubuntu 17.10 repository. Use following  command to install it.

sudo apt install php7.1 php7.1-fpm php7.1-mysql php-common php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-readline php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl

Start php7.1-fpm with this command.

sudo systemctl start php7.1-fpm

Autostart php at boot time.

sudo systemctl enable php7.1-fpm

Check status:

systemctl status php7.1-fpm

And Create a Nginx Server Block

READ THIS ALSO  How to List all Users in Ubuntu via Command Line

NGINX does not have Virtual hosts like in APACHE, it has “Server Blocks” that use the server_name and listen directives to bind to tcp sockets. To use server block, we need to remove the default symlink in sites-enabled directory.

sudo rm /etc/nginx/sites-enabled/default

Now, create a new server block file in /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/default.conf

Paste the following text in default.conf. You need to replace 12.34.56.78 with your actual server IP address.

server {
  listen 80;
  listen [::]:80;
  server_name 12.34.56.78;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

  location ~ /\.ht {
    deny all;
  }
}

Save and close the file. Now test Nginx configurations.

sudo nginx -t

If you are able to perform the test successfuly, then its time to reload Nginx.

sudo systemctl reload nginx

At last test php

At first, we need to create a info.php file in the document root directory.

sudo nano /usr/share/nginx/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. In your browser, type the localhost.info.php or ip-address/info.php
Browser will display your server’s PHP information. If you are able to see those information then php is working fine in Ngnix server.

Delete info.php file for security reason.

sudo rm /usr/share/nginx/html/info.php

Pheewwwww, it’s done. 🙂