How to install WordPress on Ubuntu 16.04

So I’ve decided to chronicle my projects for all to see on my new blog. I can’t think of a better way to christen the site than to walk though the process of installing WordPress on a server.

We’re going to need a few things to get started, like a computer to act as our webserver. I’m developing this website on my Ubuntu 16.04 laptop, and will be walking though all the steps with this OS in mind. Most Linux distros should have fairly similar steps for installing the dependencies, but you should consult google for specific instructions.

Once you have Ubuntu installed, we should install some of the back-end dependencies for WordPress to work with. According to the WordPress requirements page, we’re going to need:

I’m following this LAMP installation tutorial for the dependencies, but will be providing all the essential steps below.

Installation:

MySQL


First step is to install MySQL, which will act as our database, holding all of our posts, comments, and allows users to search the websites dynamic content. More information on how WordPress uses MySQL.

To install MySQL 5.7, execute this command in the terminal:

sudo apt -y install mysql-server mysql-client

The packages mysql-server and mysql-client are called ‘meta-packages’, they will always install the latest MySQL version that is available from Ubuntu. Currently, the latest version available MySQL 5.7.

During the installation, we will enter a root password for MySQL, but a lot of other options were set to default values, and there are test databases and anonymous users that we don’t need. Its good practice to clean these up with the mysql_secure_installation command.

mysql_secure_installation

You’ll see this set of prompts:

Securing the MySQL server deployment. 

Enter password for user root: Enter your MySQL root password 

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? 
Press y|Y for Yes, any other key for No: N

Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : Password validation enforces requirements for passwords, such as length and special characters
... skipping. 

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y 
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? (Press y|Y for Yes, any other key for No) : Y 
Success. 

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : Y


- 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? (Press y|Y for Yes, any other key for No) : Y 
Success.

Apache2


Apache 2 is available as a default Ubuntu package, and can be installed with the package manager:

sudo apt -y install apache2

Now direct your browser to localhost or your local IP such as http://192.168.0.20, and you should see the default Apache2 page. For best results, you should make your local IP static, which can be done on your Ubuntu machine or through your router. This will make the transition to a webserver, or hosting the website easier when you do your port forwarding, should you decide to self host.

PHP7


We can install PHP 7 and the Apache PHP module again with the package manager:

sudo apt -y install php7.0 libapache2-mod-php7.0

Then restart Apache:

sudo systemctl restart apache2

Test PHP and get details about your PHP installation


If you’ve seen the website load in your browser, you may be wondering “where is this file on my computer, and how do I add my own HTML files?” The default directory for the webserver is /var/www/html. We want to make a PHP file that shows us information just like Apache does when we load our webpage.

Create a file called info.php in the /var/www/html directory:

sudo nano /var/www/html/info.php

Type in these three lines:

<?php
phpinfo();
?>

To quit out of nano, press  crtrl + x, confirm the write with y and hit enter to verify the file name.

We need to change the owner and group of this file to www-data so that the server has permissions to the file we just created:

sudo chown www-data:www-data /var/www/html/info.php

Now we can get to this file in the browser (e.g. localhost/info.php):

MySQL support in PHP


To get MySQL support in PHP, we can install the php7.0-mysql package. Now is also a good time to install any other PHP modules you might need for the rest of your website. You can search the package manager for PHP modules like this:

sudo apt-cache search php7.0

Pick the ones you need and install them like this:

sudo apt -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc

Now restart Apache2:

sudo systemctl restart apache2

Install the Opcache + APCu PHP cache to speed up PHP


PHP 7 ships with a built-in opcode cacher for caching and optimizing PHP intermediate code, it has the name ‘opcache’ and is available in the package php7.0-opcache. It is strongly recommended to have an Opcache installed to speed up your PHP page. Besides opcache, I will install APCu which is a compatibility wrapper for opcache to provide the functions of the APC cache, an often used caching system in php 5.x versions and many CMS systems still use it.

Opcache and APCu can be installed as follows:

sudo apt -y install php7.0-opcache php-apcu

Don’t worry if it shows that Opcache is already installed.

Now restart Apache:

sudo systemctl restart apache2

WordPress Database initialization


Now with all the dependencies installed, we can prepare them for WordPress. We want to start by creating a MySQL database for WordPress to use. Login to the MySQL database as the root user, and put in the password when prompted:

mysql -u root -p

We want to create a database with the name wordpressdb, a user of this database with the name wordpressuser, and a password of your choosing such as wordpresspassword. The password here is just for this guide, pick your own, more secure password for your own database!

CREATE DATABASE wordpressdb;
CREATE USER wordpressuser@localhost IDENTIFIED BY 'wordpresspassword';
GRANT ALL PRIVILEGES ON wordpressdb.* TO wordpressuser@localhost;

Then exit the MySQL shell:

FLUSH PRIVILEGES;
exit

Restart Apache and MySQL:

sudo systemctl restart apache2
sudo systemctl restart mysql

Installing WordPress


With all the dependencies installed, we’re ready to install WordPress. We want to download the latest version of WordPress and unzip it to /var/www/html/. We will also want to change the owner and group of these files to www-data and set the appropriate permissions for the files.

To make the install a little bit cleaner, we can download the latest WordPress zip file to the temporary folder:

cd /tmp
wget http://wordpress.org/latest.zip

We need to unzip the file and to do so we need to get the unzip package:

sudo apt install unzip

We can unzip the WordPress zip archive directly into the /var/www/html folder:

sudo unzip -q latest.zip -d /var/www/html/

Now set appropriate permissions for the WordPress directory:

sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress

We need to create an upload directory inside the wp-content directory. This will be the parent directory of our content:

sudo mkdir -p /var/www/html/wordpress/wp-content/uploads

Like before, we need to allow the web server itself to write to this directory. We can do this by assigning user and group ownership of this directory to our web server user www-data. This will allow the web server to create files and directories under this directory, which will permit us to upload content to the server:

sudo chown -R www-data:www-data /var/www/html/wordpress/wp-content/uploads

WordPress is now installed and ready to setup!

WordPress Initial Setup:


Now proceed to the web installation of WordPress. Go to the URL localhost/wordpress/ in your web browser. The WordPress installer will show up.

Select language and press Continue:

The welcome screen shows up in the selected language. Click on Let’s go.

Type in the Login details of the WordPress database that we created in step 2. The database host is “localhost” and the prefix can be left at its default. Then click on the “Submit” button.
Wordpress saves the database configuration details to the file /var/www/html/wordpress/wp-config.php. Click on “Run the install” to proceed to the next part of the installer.
Now enter some details for the Blog like website title, admin username, password and email address. I used these values:

Site Title = My WordPress Blog
Admin Email = admin@example.com 
Username = admin
Admin password = password

The above values are just examples and you should use the real title and email address here. Using admin as administrator name is ok for an internal website but you might want to use a nonstandard name or your personal name instead.
The press Install WordPress to finish the installation:
Now we will proceed towards the login page by pressing Log In:
Type in the credentials that you selected during WordPress installation. The WordPress Dashboard will show up.
You’re in! From here you can choose one of the many free WordPress themes and apply them under the Appearance tab and then you can start writing blog entries under the Posts tabs.
That’s it for this post, I’ll be going over some of my customizations that I’ve done to my blog in another post. Feel free to ask any questions below in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *