Today I’m going to show you how to set up a LEMP server (Linux NginX MariaDB PHP) on an Ubuntu 14.04. It’s a relatively easy process once you’ve done it a few times, but there are some things that can trip you up, so I thought I’d document it and present it here.
For this tutorial I used a Digital Ocean Droplet, but any Ubuntu 14.04 server should work the same way.
Set up a privileged user
This assumes you’re using a fresh install of Ubuntu 14.04, and it’s not a crucial step in the process if your server is already set up. But we want to create an environment with a privileged user and prevent people from being able to login as root over SSH. This is just an extra precaution I like to do on servers I set up.
Log in to your user as root, and type in the following:
It doesn’t have to be “web” you can call it anything you want.
Add a password and there will be some prompts where you can add as much or as little information as you want for the account.
Then run the following command:
and add the new account:
It should look like this:
Ctrl + X to exit, Y to save the file
Now we want to edit the SSH settings:
Change the Port to something between 1025 and 65536. This just adds an extra step for attackers, and bots that scan for open port 22 won’t find your connection immediately.
Look for this line:
and change it to
This prevents people from logging into SSH as root.
As an additional step, let’s add the line:
so only the web user is allowed to log on to the server.
Restart the ssh server:
Now before we log out, we want to test and make sure we can get back in.
4400 is the port shown here, but it can be whatever you choose.
If you can log in fine, then you’ll be safe to exit out of your main session, and log back in as “web” (or whatever you used as a username).
Install the Web Server
first type in
to update the system.
To install NginX:
In Ubuntu 14.04 it will automatically start up the service, and you should see this startup page:
Now your NginX server is set up so we’ll move on and modify it later.
Install the Web Server
We’re going to install MySQL on the server, but we’ll be using the MariaDB fork instead of the MySQL distribution, because it’s better in many ways.
For our install we’re using Ubuntu 14.04 and I want MariaDB 10, so here is how I set it up:
1 2 3
If you want to use something different or are working with a different OS, check here to see which repo to use.
Now we want to secure up our install a bit:
enter your root password and answer “Y” to all the things you want to remove for security purposes.
Now you have a (MySQL compatible) MariaDB instance set up!
Install and configure PHP
Now we want to install PHP and get it set up with NginX.
Now, we need to configure PHP:
Look for the following in the php.ini file:
remove the semicolon and set it to 0:
Quit and save the file.
Now we need to make a small change to the PHP-FPM config:
look for the listen directive and make sure it says:
Quit and save the file, then restart PHP5-FPM:
Now you’re set up!
Create a folder where your web files will be stored. I generally set up something like this:
Now open up this file:
The default server setup looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
Make the following changes to the config so it looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Replace /var/www/yourdomain.com/public with whatever folder you’d like to use for your website (what you created earlier), and replace server_domain_name_or_IP with your server domain name or IP.
Now restart your web services:
Now we can create a test file to check your PHP configuration:
Add the following:
Now bring up the page in a browser and you should see this:
And you’re done!! Now you can add in your files and start building websites or applications. In future articles I’ll show some good deployment methods for your new website.