HowTo: Self Hosted WordPress on Ubuntu 16.04LTS

FEATURED

Self-Hosted WordPress on Ubuntu 16.04LTS

There are a lot of good reasons to use a hosting provider for your website or blog. A hosting provider will give you a platform to host your site without the responsibility of maintaining a server or needing to know the low level configuration details of the server software and WordPress.

But, if you’re a little bit technical, want to learn to be a little bit technical, or just want to get your site off the ground for free, self hosting on a VPS (virtual private server) is a great option. Both Google Cloud Platform (GCP) and Amazon Web Services (AWS) offer free servers for at least a year. Google recently added an Always Free option for a small VPS.

Both the free options are more than powerful enough to start self-hosting a small website. Both are scalable so if your website grows your infrastructure can grow with it, or you can move to a reputable manager service provider.

Both providers offer Ubuntu 16.04LTS virtual servers. In this post we are going to walk through setting up WordPress on Ubuntu 16.04LTS. WordPress requires a few pieces of software. You will need web serving software, PHP, and a database.

In light of you being a little bit technical I’m going to assume you already have a domain (or are comfortable using the IP address to access your server temporarily) and understand how DNS works.

WE WILL USE

  • Ubuntu 16.04.2 LTS
  • WordPress 4.7.3
  • Nginx (web server)
  • PHP7.0-FPM (PHP processing)
  • MariaDB (MySQL compatible database server)

Installing the Server Software

We need to install and configure Nginx, PHP and MariaDB before we can setup WordPress. To simplify things, we are going to sudo su - and run all the commands in this post as root.

Install the required software packages

apt install nginx php-fpm php-mysql mariadb-server
...
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libaio1 libcgi-fast-perl libcgi-pm-perl libdbd-mysql-perl libdbi-perl
  libencode-locale-perl libfcgi-perl libfontconfig1 libgd3 libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl libjbig0 libjpeg-turbo8 libjpeg8
  liblwp-mediatypes-perl libmysqlclient20 libterm-readkey-perl libtiff5 libtimedate-perl liburi-perl libvpx3
  libxpm4 libxslt1.1 mariadb-client-10.0 mariadb-client-core-10.0 mariadb-common mariadb-server
  mariadb-server-10.0 mariadb-server-core-10.0 mysql-common nginx nginx-common nginx-core php-common php-fpm
  php-mysql php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-mysql php7.0-opcache php7.0-readline

Configure Nginx and php7.0-fpm

Next we have to uncomment a few lines to process PHP scripts properly in the default nginx site configuration.

Open /etc/nginx/sites-enabled/default with your preferred text editor (#vi4life), add index.php to the index list, and uncomment the following lines.

vi /etc/nginx/sites-enabled/default

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;

...

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
        include snippets/fastcgi-php.conf;
#
#       # With php7.0-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

We also need to enable php7.0-fpm and nginx to run at system start and we’ll go ahead and start both services so we can verify everything is working the way we expect right now.

Enable and start services

# enable service start on boot
systemctl enable php7.0-fpm
systemctl enable php7.0-fpm
# start services
systemctl start php7.0-fpm
systemctl start nginx

At this point you should have nginx and PHP configured to run on your server. We can verify this by using the server’s IP address or your domain name in a web browser.

Before doing that let’s create a PHP file to make sure we’re processing PHP scripts. Change directory into /var/www/html/ and create a file named test.php with the phpinfo() function as contents.

test.php

<?php phpinfo(); ?>

The phpinfo() function will display some information about your version and configuration of PHP on your system. The details aren’t important right now. What we’re looking to do is verify that your system is processing PHP files properly and displays the phpinfo() page. If you are prompted to download the file test.php there is a problem.

You most likely need to double check your /etc/nginx/sites-enabled/default file.

If you’re on track you should see the phpinfo page.

phpinfo

Configure MariaDB (MySQL)

MariaDB is a fork of MySQL that was created after Oracle took over MySQL. If you prefer to use MySQL these steps should work just fine, but I am not going to test them. If you do and have problems please let us know in the comments.

Alright, first things first. We’ve got MariaDB installed, but we need to configure the root password and clean up the default installation. The easiest way to do this is by using the mysql_secure_installation script. This is installed as part of MariaDB.

The script will ask for the current MySQL root password and you an hit Enter to continue. The next question will ask if you want to change the root password. Yes, you do. Enter your desired password and then confirm and you can plow through the rest of the script accepting the defaults.

mysql_secure_installation

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Creating the WordPress database

Next we will create a database and grant wpuser access to the database.

# mysql -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 40
Server version: 10.0.29-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on wordpress.* to wpuser@localhost identified by 'strongpassword';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

Installing WordPress

To install WordPress we will need to download it to the server, extract it to the website document root, create our wp-config.php, and run the install.

The easiest way to download the latest version of WordPress to your server is running cd; wget https://wordpress.org/latest.tar.gz.

# cd; wget https://wordpress.org/latest.tar.gz
--2017-03-17 20:05:11--  https://wordpress.org/latest.tar.gz
Resolving wordpress.org (wordpress.org)... 66.155.40.249, 66.155.40.250
Connecting to wordpress.org (wordpress.org)|66.155.40.249|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8008833 (7.6M) [application/octet-stream]
Saving to: ‘latest.tar.gz’

latest.tar.gz                100%[=============================================>]   7.64M  6.19MB/s    in 1.2s

2017-03-17 20:05:12 (6.19 MB/s) - ‘latest.tar.gz’ saved [8008833/8008833]

Now we’ve got wordpress and we need to extract the files into our document root directory. We’re going to delete the files in /var/www/html/, extract the WordPress archive, and then copy the files into /var/www/html.

Install WordPress files

rm -rf /var/www/html/*
tar xf latest.tar.gz
cp -r wordpress/* /var/www/html/

Configure WordPress

configure-wordpress

We will now go open a browser and enter our URL. You will be greeted with the ‘Welcome to WordPress’ page. From here we will setup the database and wp-config.php file. Let's Go

Enter the database user and password you created earlier in this guide and click Submit.

configure-wordpress

We are now asked to create the wp-config.php file. You can copy the text and paste it into /var/www/html/wp-config.php. Once you’ve done that click Run the install.

configure-wordpress

One more page to fill out before we can call it complete.

configure-wordpress

After running the install you should be able to login. Now you’re ready to familiarize yourself with WordPress, select a theme, customize, and start posting.

Good luck!

comments powered by Disqus