An Easy-To-Follow Guide in Setting Up WordPress with Nginx on Ubuntu 14.04 Server (LEMP)
Running WordPress on an Ubuntu 14.04 with Nginx as your web server application is a solid box setup to host your blog in a VPS with minimal resources.
Summary:
- Initial Server Setup
- Install CSF
- Install LEMP
- Configure Nginx Server Blocks
- Install Free SSL Using Let’s Encrypt
- Install WordPress
- Sources
1. Initial Server Setup
The installation and configuration of the necessary software would be as easy as 1-2-3 when you follow the ste-by-step guide in this post. This section will tackle the initial server configuration using DigitalOcean’s 512MB Droplet ($5 per month plan). Get a free $10 credit when you register using this link.
Fire up your Droplet with Ubuntu 14.04 as your base OS. Do not use a private key for the meantime. The root password would be sent to your email. Once you have booted your Droplet, connect to it using through ssh.
1 2 3 4 5 6 |
ssh root@SERVER_PUBLIC_IP Current UNIX Password: Enter the password that was sent in your email Enter new UNIX Password: Enter a new strong password Reenter new UNIX Password: Reenter your new strong password |
The very first thing we need to do after logging into your box is to create a non-root user and grant it sudo privileges.
1 2 |
adduser USERNAME gpasswd -a USERNAME sudo |
Replace USERNAME
with your username of choice.
Edit your BASH profile (~/.profile
) to add timestamp to your history and avoid duplicates.
1 2 3 4 5 6 7 |
PS1='${debian_chroot:+($debian_chroot)}\[\e[1;31m\]\u@\h\[\e[1;34m\]:\[\e[00m\]\w\$ ' HISTCONTROL=ignoreboth HISTTIMEFORMAT='%F %T ' export PS1 export HISTCONTROL export HISTTIMEFORMAT |
Note: PS1
changes are just cosmetic customization of your BASH prompt. Edit as you like.
Adjust your timezone. I prefer using UTC as a standard time of all my servers.
1 |
dpkg-reconfigure tzdata |
In the setup screen, choose the timezone you prefer. You may set it to your local timezone if that’s what you prefer.
Secure your SSH server by removing it off from the default port which is port 22. You may choose whatever non-common port number you like. You may want to choose a number between 1024-65535. Open your ssh config file /etc/ssh/sshd_config
and edit the following
1 2 3 4 5 |
Port CUSTOM_PORT Protocol 2 PermitRootLogin without-password PasswordAuthentication no UseDNS no |
Replace CUSTOM_PORT
with any port number you like. I advise you not to use any of the well-known ports.
Finally, update your server.
1 |
apt-get update && apt-get -y upgrade && apt-get -y dist-upgrade |
2. Install CSF
Config Server Firewall (CSF) is a free firewall management software designed for Linux distributions. In this section, we would install CSF as well as configure the basic settings and enable its User Interface panel which you can access using your web browser.
First, we need to disable the default firewall configuration tool that comes with Ubuntu.
1 |
ufw disable |
Next, let us download the latest CSF release from their website.
1 2 3 4 5 6 |
cd /usr/src wget https://download.configserver.com/csf.tgz tar zxvf csf.tgz cd csf sh install.sh |
After the installation, let’s go ahead and edit some basic settings in its configuration file /etc/csf/csf.conf
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
TESTING = “0” RESTRICT_SYSLOG = “3” SYSLOG_CHECK = “3600” LF_POP3D = “1” LF_IMAPD = “1” UI = “1” UI_PORT = “UI_PORT” UI_USER = “UI_USERNAME” UI_PASS = “UI_PASSWORD” UI_RETRY = “3” #Append UI_PORT in TCP_IN # Set Ubuntu Log Files SU_LOG = "/var/log/syslog" FTPD_LOG = "/var/log/syslog" IPTABLES_LOG = "/var/log/syslog" SUHOSIN_LOG = "/var/log/syslog" BIND_LOG = "/var/log/syslog" SYSLOG_LOG = "/var/log/syslog" SSHD_LOG = “/var/log/auth.log” SMTPAUTH_LOG = “/var/log/auth.log” |
Change the UI_USERNAME
, UI_PASSWORD
, and UI_PORT
as you like.
Now let us install the CSF UI dependencies.
1 |
apt-get install libwww-perl |
Finally, reload CSF and restart LFD.
1 2 |
service lfd restart csf -r |
You would now be able to access your CSF UI by going to
https://SERVER_PUBLIC_IP:UI_PORT
3. Install LEMP (Nginx, MariaDB, PHP)
Normally, users would choose to install Apache instead of Nginx due to its popularity. However, I decided to install Nginx on the 512MB Droplet due to its tight resources. Out-of-the box, Nginx uses less resources compared to Apache. Let’s proceed in installing these software to get our server ready in hosting our WordPress website.
Login using the non-root user you created in first step of this guide. Install the Nginx from the Ubuntu repository.
1 |
apt-get install nginx |
Next, we install MariaDB (or MySQL). I prefer using MariaDB due to its speed and performance improvements over MySQL. Below are two separate steps you could follow depending on whichever you decided to install.
# MariaDB Installation
1 2 3 4 5 6 7 |
sudo apt-get install software-properties-common sudo apt-key adv —recv-keys —key server hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db sudo apt-get install mariadb-server sudo service mysql stop sudo mysql_install_db sudo service mysql start sudo mysql_secure_installation |
# MySQL Installation
1 2 3 |
sudo apt-get install mysql-server sudo mysql_install_db sudo mysql_secure_installation |
Nginx does not include a native PHP processor. Thus, we need to install php5-fpm
.
1 |
sudo apt-get install php5-fpm php5-mysql |
Edit the configuration file /etc/php5/fpm/php.ini
and find the following line and set it to “0”
1 |
cgi.fix_pathinfo=0 |
Now let us configure Nginx to use the newly installed PHP Processor
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 |
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; server_name SERVER_DOMAIN_NAME_or_PUBLIC_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; } } |
Make sure to add index.php
and edit SERVER_DOMAIN_NAME_or_PUBLIC_IP
to match your domain name or IP address. Also add the block of text highlighted in red inside your server block.
Finally, restart Nginx and PHP-FPM afterwards. I encountered an issue where the PHP processor is not working properly. If that is the case. Restart your server to fix it.
1 2 3 |
sudo service nginx restart sudo service php5-fpm restart sudo shutdown -r now |