LEMP Stack Banner

Installing LEMP Stack On Vultr VPS With CentOS 7

Continuing my previous post about buying VPS in Vultr, now I’m going to share how to set up my VPS to install my web app. I developed my web app using Laravel which is a PHP framework and using MySQL as my RDBMS. So, in my VPS I need to install either LAMP stack or LEMP stack. LAMP (Linux, Apache, MySQL, PHP) and LEMP (Linux, Engine-X, MySQL, PHP) are the most popular group of open source software to run your web server. Their only difference is the web server engine, where LAMP contain Apache and LEMP contain Engine-X (Nginx). I prefer using Nginx as web server engine because based on what I read, Nginx performance won’t drop in handling massive web traffic requests while Apache will. Therefore, I’m going to provide steps to install LEMP.

 

1. Installing Linux

As I stated previously on my post, I’ve already chosen CentOS 7 as my VPS operating system. After paying that VPS, Vultr automatically installed the CentOS for me. The only thing that I need to do is login to the server as root user using PuTTY. PuTTY is a great Secure Shell (SSH) client that help Windows users to access their servers remotely. You can download PuTTY here. If you are a Linux or Mac user you can access your server using your terminal. After installing PuTTY, open it and fill the host name with your VPS IP address, choose SSH as connection type and put 22 as the port number then click Open button. PuTTY shell window then will appear, login as root and enter your password that you’ve got from your Vultr VPS server information.  FYI, Vultr server password is generated randomly, so it’s better to click copy button that is located beside the password and paste it by right clicking inside your PuTTY shell. If you can’t see the password, don’t worry because you won’t see any password asterisks (*) appear on CentOS, so just press enter. Great! Now you are inside your server as root and have privilege as super admin.

PuTTY and Its Shell
PuTTY and Its Shell

The first thing that you must do after the CentOS fresh install is update all your CentOS packages using yum. Basically, yum is CentOS package manager that will help you to install, update and remove all applications that you need in your server. Type command below on your PuTTY shell to update every package.

yum update

You’ll see a lot of text appear like crazy. Don’t worry, if there are a lot of packages that obsolete all of them will get updated one by one, just wait and relax. After that, add CentOS Extra Package for Enterprise Linux (EPEL) repository using following command.

yum install epel-release

That EPEL repository is maintained by the Fedora team and will provide open source package with no compatibility issue.

 

2. Installing Engine-X (Nginx)

Next, install Nginx. You can use below command to install it.

yum install nginx

After it finish, you have to start the nginx using below command then you can open your Vultr IP Address as URL on your web browser. It’ll show “Welcome to nginx” message.

systemctl start nginx

I suggest that you also set nginx to start automatically after rebooting the server by typing this command.

systemctl enable nginx

Awesome! Now we have nginx installed we can continue to install MySQL.

 

3. Installing MySQL

CentOS 7 will install MariaDB instead of MySQL if you run yum install mysql command. So, if you really need MySQL you need to know the MySQL’s rpm file version for the CentOS 7. You can check the latest version in https://dev.mysql.com/downloads/repo/yum/. Copy the rpm file name, in this case mysql57-community-release-el7-11.noarch.rpm  and run this command on your shell.

MySQL 5.7 RPM
MySQL 5.7 RPM
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

After you get the rpm file, it is important to check MD5 value of the file with the one on their site to make sure the file is not corrupted.

md5sum mysql57-community-release-el7-11.noarch.rpm

After running command above, compare the MD5 that appear with the one provided on their website. If it the same than you are good to go.

Next, you need to add the rpm file to yum repository by typing this command.

rpm -ivh mysql57-community-release-el7-11.noarch.rpm

Then, you can install the MySQL Server using this command. Press y if you are asked to accept GPG key.

yum install mysql-server

Start MySQL daemon using command below.

systemctl start mysqld

And see if it is successfully running using following command because systemctl won’t show any output for start/stop command.

systemctl status mysqld

If it shows Active: active (running) with green text color then you can continue to configure MySQL.

Before configuring it, we need to get the temporary password that is generated when you install MySQL. Type this command and take a note of the password because we need it later.

grep 'temporary password' /var/log/mysqld.log

Run this command to secure MySQL installation. This command will ask you a lot of things including your new root password and remove default options.

mysql_secure_installation

First, you will be asked the temporary password that I asked you to take a note before, input it. Then, you will be asked for new password. The rule of the new password is it has to be 12 characters that contains at least one uppercase letter, one lowercase letter, one number and one special character.  When there is a prompt to change your password again press other key other than y for no.  After that, type y and enter for all questions prompt. Those questions will ask you to remove anonymous users, disallow remote root login, remove the test database and access to it, and reload the privilege tables.

Now, check if your MySQL has been installed and configured correctly. Type this command, -u means user so it is root and -p is password so type your new MySQL password.

mysqladmin -u root -p version

If everything is okay, then you’ll see some information regarding your MySQL including the version. MySQL automatically enable itself every time server boot by default, so you don’t need to enable it again. Nice! Next we will install the last part of LEMP stack which is PHP.

 

4. Installing PHP

My web app uses Laravel as PHP framework. The latest Laravel framework only support PHP 7 and above. So, in my server I decide to install PHP 7 instead of PHP 5.6. However, CentOS 7 repositories doesn’t support PHP 7 yet, that’s why I looked for third party PHP 7 repositories. There are 3 famous PHP 7 third party repositories for CentOS, they are Webtatic, IUS, and Remi’s repositories. In this case, I used Webtatic’s repositories. I can’t talk much about the others, because I’ve never used any of them. Installing PHP 7 on CentOS 7 is not that difficult. First, you need to add Webtatic repositories to yum using command below.

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Then, install PHP 7 using following command. This, command will install PHP 7.0, if you need to install PHP 7.1 you can change php70w to php71w or read this Webtatic site for further information.

yum install php70w

After the process has been done, you can continue to install additional PHP 7 modules. The modules that you need is FPM, MySQL and MbString (Laravel need this) modules. Install them all using this command.

yum install php70w-fpm php70w-mysql php70w-mbstring

Next, you need to configure PHP in php.ini file. Type this command to edit the content.

nano /etc/php.ini

The only thing that you need change for now is cgi.fix_pathinfo. Uncomment it and change the value to 0.

cgi.fix_pathinfo=0

Next, go to www.conf file by typing this command.

nano /etc/php-fpm.d/www.conf

Find all row that contain information about listen, listen.owner, listen.group, user, and group. Change the value as shown below.

listen = /var/run/php-fpm/www.sock
listen.owner = nobody
listen.group = nobody
user = nginx
group = nginx

Yup, that’s all, now start your PHP-FPM using command below.

systemctl start php-fpm

Don’t forget to enable it so it automatically start after the server booting.

systemctl enable php-fpm

Great! We have LEMP installed now, but it can’t work yet because there is one thing that we need to do. It is configuring Nginx for PHP.

 

5. Configuring Nginx For PHP

First thing first, you have to open Nginx default.conf file by typing this command.

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

Then, change the server part so it looks like configuration below. In server_name part change the value to your VPS IP address or if you’ve already have domain name for it you can use it. In the root part, I use /var/www (I forgot if this folder already there or not, so if it’s not there just make one). It means every time your IP address or domain opened on the browser, Nginx will look for index file of your web in that folder. For fastcgi_pass value make sure it is same with value of listen in www.conf file that you’ve configured before.

server {
  listen 80;
  server_name server_IP;

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

  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_pass unix:/var/run/php-fpm/www.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

Restart your Nginx using below command, so the change in configuration can be applied.

systemctl restart nginx

Cool! Now, it’s time to test if every configuration that you’ve done working properly. You can make one php file in the root folder which is /var/www using command below.

nano /var/www/info.php

Just make simple php program, for example, showing your php info using phpinfo() like below.

<?php 
  phpinfo(); 
?>

Open your browser, type your VPS IP address or domain if you’ve already had one in the url bar. If it shows PHP information then everything is configured correctly, if not probably you need to recheck every configuration above or find out the cause of error from error.log file using following command.

nano /var/log/nginx/error.log

By the way, if everything has worked as expected don’t forget to delete those php file that you’ve just created using command below.

rm /var/www/info.php

Congrats! Now your VPS is ready to host your web app.

 

I’m not a native English speaker. Writing this post is part of practicing my English. Feel free to give feedback in the comment below about my post or if there is any grammar mistake or misused vocabulary. Thank you 🙂

Share this post with your friends

Oger Vihikan

I study about Information Technology and really interested in trying new thing that related to that subject. I use this blog to share what I've learned after trying it. I hope this blog could give you some useful information. Happy reading :)