How To Install Wordpress on Ubuntu 18.04 Server

How To Install Wordpress on Ubuntu 18.04 Server
Photo by Fikret tozak / Unsplash

Introduction

WordPress is one of the most popular content management systems (CMS) in the world, powering over 30% of all websites.1 It’s popularity is largely due to it’s ease of use and vast plugin ecosystem. While you can install WordPress on various operating systems, in this guide we will focus on installing WordPress on an Ubuntu 18.04 server.

Prerequisites

Before you begin this guide you’ll need the following:

An Ubuntu 18.04 server with a non-root user with sudo privileges. You can learn how to set this up by following Steps 1-3 of How To Set Up a Host Name with DigitalOcean.

A domain name pointed at your server’s public IP address. In this guide we will be using example.com . You can learn how to set this up by following How To Set Up a Domain Name with DigitalOcean.

Step 1 — Installing Apache and PHP

The first thing we need to do is install some dependencies including Apache and PHP. WordPress requires a web server and PHP to function.

We can install all of the dependencies that we need for WordPress in one go by typing:

sudo apt-get update sudo apt-get install apache2 php libapache2-mod-php php-mcrypt php-mysql

Once the installation is complete, we can verify that Apache is running by visiting our server’s IP address in our web browser. You should see the default Apache web page which looks like this:

Now that we have our web server up and running, we can move on to the next step.

Step 2 — Installing MySQL

The next dependency that we need to take care of is a database. WordPress uses MySQL to store and retrieve all of its data.

We can install MySQL with the following command:

sudo apt-get install mysql-server

During the installation process, you will be asked to set a password for the MySQL root user. This is the administrative account for your MySQL installation and it has full control over all of the databases. It’s very important that you choose a strong, unique password for this account.

After the installation is complete, we need to secure our MySQL installation by running the mysql_secure_installation script. This script will guide you through a few changes that will harden the security of your MySQL installation.

To start the script, type the following command:

sudo mysql_secure_installation

You will be prompted to enter the password that you set for the MySQL root user. After that, you will be presented with a series of questions. It’s recommended that you answer them all in the affirmative to harden the security of your MySQL installation.

Once the script has finished, you can move on to the next step.

Step 3 — Creating a WordPress Database and User

Now that we have MySQL installed, we can create a database and user for our WordPress application.

Log in to the MySQL shell by typing the following command:

sudo mysql

You will be prompted for the MySQL root password that you set up in the previous step.

First, we need to create a database for WordPress to use. We can create a database for WordPress with the following command:

CREATE DATABASE wordpress;

Next, we need to create a MySQL user account that we will use to connect to and manipulate the database. It’s considered best practice to create a separate MySQL user for each application that you install. That way, if one application is compromised, the attacker will not automatically gain access to your other applications.

We can create a new MySQL user with the following command:

CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';

In this command, we are creating a user called wordpressuser with the password password . You should replace these values with a strong username and password of your own.

Now that we have a database and user set up for WordPress, we need to give our WordPress user access to the database. We can do this by running the following command:

GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

This command will give our WordPress user full control of the wordpress database.

Now that we have our database and user set up, we can flush the privileges so that the changes we have made will take effect. We can do this by running the following command:

FLUSH PRIVILEGES;

Now that we have our database and user set up, we can exit the MySQL shell:

EXIT;

Step 4 — Downloading WordPress

Now that we have all of the dependencies that WordPress needs installed, we can download the WordPress files from the WordPress.org website.

We can do this with the following command:

wget [<http://wordpress.org/latest.tar.gz>](<http://wordpress.org/latest.tar.gz>)

Once the download is complete, we can extract the files from the archive that we just downloaded with the following command:

tar xvfz latest.tar.gz

This will extract the WordPress files into a directory called wordpress in the current directory.

Step 5 — Configuring WordPress

Now that we have the WordPress files downloaded and extracted, we can configure WordPress to connect to our database.

First, we need to copy the sample configuration file so that we can edit it:

cp wordpress/wp-config-sample.php wordpress/wp-config.php

Next, we need to open the wp-config.php file in a text editor. We will be using nano :

nano wordpress/wp-config.php

First, we need to set the database configuration options. We can find these options towards the top of the file. We need to set the following options:

define('DB_NAME', 'wordpress');

This line should already be set to wordpress which is the name of the database that we created in Step 3.

Next, we need to set the database user:

define('DB_USER', 'wordpressuser');

We created this user in Step 3. Be sure to replace wordpressuser with the actual username that you created.

Next, we need to set the database user’s password:

define('DB_PASSWORD', 'password');

Again, be sure to replace password with the actual password for the database user.

Next, we can set the default database host. In most cases, this should be set to localhost :

define('DB_HOST', 'localhost');

If you are using a remote database, you will need to set this value to the IP address or hostname of your database server.

Next, we need to set the WordPress secret keys. These are used to enhance the security of information passed through the cookies used by WordPress. You can generate a unique set of secret keys by visiting the WordPress secret key generator.

Once you have generated a set of keys, you can copy and paste them into the wp-config.php file, replacing the existing dummy keys. It should look something like this:

define('AUTH_KEY', 'put your unique phrase here'); 
define('SECURE_AUTH_KEY', 'put your unique phrase here'); 
define('LOGGED_IN_KEY', 'put your unique phrase here'); 
define('NONCE_KEY', 'put your unique phrase here'); 
define('AUTH_SALT', 'put your unique phrase here'); 
define('SECURE_AUTH_SALT', 'put your unique phrase here'); 
define('LOGGED_IN_SALT', 'put your unique phrase here'); 
define('NONCE_SALT', 'put your unique phrase here');

Now that we have set all of the necessary configuration options, we can save and close the file.

Step 6 — Copying the WordPress Files

Now that we have WordPress configured, we can copy the WordPress files to the correct location.

By default, Apache is configured to serve files from the /var/www/html directory. We can copy the WordPress files to this directory with the following command:

sudo cp -r wordpress/* /var/www/html/

Now that the files are in the correct location, we need to adjust the ownership and permissions of the files. WordPress needs to be able to read and write to the WordPress files.

We can adjust the ownership of the files with the following command:

sudo chown -R www-data:www-data /var/www/html/

Next, we can adjust the permissions of the files with the following command:

sudo chmod -R 755 /var/www/html/

Now that we have adjusted the ownership and permissions of the WordPress files, we can move on to the next step.

Step 7 — Configuring Apache for WordPress

Now that we have WordPress installed, we need to configure Apache to serve the WordPress files.

We can do this by creating an Apache configuration file.

We will start by creating a new directory where we can store our Apache configuration files. We can do this with the following command:

sudo mkdir /etc/apache2/sites-available

Next, we can create a new configuration file for our WordPress site in this directory with the following command:

sudo nano /etc/apache2/sites-available/wordpress.conf

Inside, we will need to add the following lines:

<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/ ServerName [example.com](<http://example.com/>) ServerAlias [www.example.com](<http://www.example.com/>) <Directory /var/www/html/> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Be sure to replace example.com with your own domain name.

When you are finished, save and close the file.

Next, we can enable our new WordPress site by typing the following command:

sudo a2ensite wordpress

Now that our site is enabled, we need to disable the default Apache site. We can do this by typing the following command:

sudo a2dissite 000-default

Now that we have our site configured, we can test our configuration file for syntax errors by typing the following command:

sudo apache2ctl configtest

You will see the following output:

Output Syntax OK

If you see this output, it means that there are no syntax errors in our Apache configuration file.

Now that we have verified that our configuration file is free of syntax errors, we can restart Apache to load the new configuration by typing the following command:

sudo systemctl restart apache2

Step 8 — Completing the WordPress Installation

Now that we have our web server configured to serve the WordPress files, we can complete the WordPress installation through the web interface.

To do this, we need to visit our domain name in a web browser. You will be redirected to the WordPress setup wizard:

Select your preferred language and click Continue.

On the next page, you will need to provide some information about your WordPress installation. First, you will need to provide the database connection information. This is the information that we set up in Step 5.

The Database Name, Username, and Password fields should be pre-filled. If not, fill in the database name ( wordpress ), username ( wordpressuser ), and password that you created earlier. The Database Host field should be set to localhost .

Next, you will need to set the Table Prefix field to wp_ . This is the default table prefix used by WordPress.

When you are finished, click Submit.

If the database connection information is correct, you will be taken to the next page of the setup wizard. On this page, you will need to provide some information about your site.

First, you will need to set the Site Title. This is the name of your WordPress site.

Next, you will need to set the Username, Password, and Email fields. These fields will be used to create an administrative account for your WordPress site. Be sure to choose a strong password for the administrator account.

When you are finished, click Install WordPress.

If the installation is successful, you will see the following page:

Click Log In to be taken to the WordPress login page.

On the WordPress login page, enter the administrator username and password that you set up earlier.

Once you are logged in, you will be taken to the WordPress admin dashboard. This is where you can manage all aspects of your WordPress site.

Conclusion

In this guide, we have installed WordPress on an Ubuntu 18.04 server. We have also configured Apache to serve the WordPress files and set up a domain name to point to our server.