[How to] Create a LEMP stack Image


#1

This page shows you how to create a LEMP stack Image.

Requirements

LEMP stack is a group of open source software to get all tools to run a web application.
The acronym stands for Linux, nginx (pronounced “engine-x”) , MySQL, and PHP.

The image you will create would allow you to create servers with predefined configuration for the LEMP stack.

For instance, you will be able to deploy an infinity of servers serving the LEMP stack in seconds.

There are five steps to create a LEMP Image

  • Create a new server
  • Install required packages
  • Configure nginx
  • Check PHP and MySQL installation
  • Create the LEMP Image

Step 1 - Create a new server

Before starting, click the “Create Server” button in the control panel.

Control Panel

You will land on the server-creation page where you must input basic information for your server:

  • The name of your server, for example LEMP
  • The tag you want to assign on it (Optional). Tags let you organize your servers, you can assign any tag to each server

Create server basic information

After inputting your server basic information, you have to choose a starting Image for your server. Use the Ubuntu Trusty 14.04 Image to create the LEMP stack Image.

Click the “Create Server” button. This action creates and starts your server. In a few seconds, your server will be ready to use.

Step 2 - Install required packages

When your server is running, you can log into your server using its public IP address.

In the terminal program and in the shell run the apt-get update command to update packages list.

The next step is to install packages required for the LEMP stack. In a terminal execute the following command

apt-get install nginx mysql-server php5 php5-cli php5-fpm php5-mysql

Important: During the packages installations, you will be asked to set a root password for MySQL server.

Step 3 - Configure nginx

Once the packages installation is achieved, you have to configure nginx to work properly with PHP

In the directory /etc/nginx/sites-available, create a new file and fill it with the configuration for php. In this guid, I called this file php_default

server {
  listen   80;


  root /var/www;
  index index.php index.html index.htm;

  server_name localhost;

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

  error_page 404 /404.html;

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
        root /usr/share/nginx/www;
  }

  # pass the PHP scripts to FastCGI server listening on the php-fpm socket
  location ~ \.php$ {
          try_files $uri =404;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
        }

}

Change to /etc/nginx/sites-enabled directory. This directory contains the enabled virtual hosts.

Remove the default virtual host, enable the php_default virtual host and reload nginx configuration.

rm /etc/nginx/sites-enabled/default
ln -s ../sites-available/php_default .
service nginx reload

Step 4 - Check PHP and MySQL installation

The LEMP stack is properly installed and configured. You have to check everything is working on.

Connect your MySQL server

mysql -u root -p
<you are asked for your password>

Create the root web directory and create a file index.php displaying the result of phpinfo()

mkdir /var/www
echo "<?php echo phpinfo(); ?>" > /var/www/index.php

Php info

In your browser, try to access to the public IP address of your server. You should see the php information on the screen as above.

Step 5 - Create the LEMP Image

You are now ready to create your LEMP Image.

Clean the log files, remove the bash history file and power off the server.

find /var/log -type f -delete
history -c
halt

In the Control Panel, on the server page set your server to OFF

Poweroff server

Once your server is powered off, in the volumes list, click the “Snapshot” button on the volume to Snapshot.

Volume snapshot

In the Control Panel, click “Snapshot” in the compute section.
Click the name of your Snapshot and rename the Snapshot to LEMP Snapshot

LEMP volume snapshot

Select the Snapshot and click the “Create an image from Snapshot” button. Set your image name as LEMP and validate.

Create a new server using the LEMP Image and power it on.

New LEMP server

Once your server is started, it contains the LEMP stack as you configured it.

You now can deploy an infinity of servers using this Image and serving the LEMP stack in seconds.


#2

Hi Edouard,

Thanks for this tuto.

I you permit, some small mistakes in what you wrote:

- In step 3:

It’s sites-available instead of site/available.

It’s sites-enabled instead of site-enabled

- In step 4:

It’s echo instead of echo.

I hope this will help.

You can remove this post once corrected.

Regards,

Bertrand.


#3

Hi @bdhebrail,

It’s fixed. Thanks for your feedback.

Edouard


#6

Just 1 more typo to correct:

ln -s ../sites-available/php_defaut 

should be

ln -s ../sites-available/php_default

#7

It’s fixed. Thanks for your feedback.


#8

Also, wouldn’t it be better in your php_default file to have:

server_name localhost;

rather than

server_name example.com;

If somebody is trying to get something up and running localhost will work but example.com probably won’t and it’s easy to miss that the server name should be changed.