How to Setup WordPress Website with Apache, MySQL and PHP on Ubuntu 18.04 VPS

Since May 2003, WordPress has remained the most popular, stable, secure and easy to use Content Management System (CMS) for setting up websites.

The free Open-source software is fully built-in and comes with thousands of beautiful themes and plugins for extended functionalities.

WordPress WYSIWYG interface is browser-based and does not require complicated FTP or HTML coding software applications to launch a website.

Due to its clean and simple code, the CMS is loved by all major search engines because it is easier to index.

WordPress is based on PHP and MySQL and runs on most popular web servers including Apache and Nginx.

On a shared hosting, WordPress can be installed by automatic script installers such as Softaculous.

However, the installation method on a VPS hosting  takes a new approach and can be done with just a few and simple shell commands.

This is a step-by-step guide on setting up a WordPress website with  Apache, MySQL and PHP on Ubuntu 18.04 VPS.

Prerequisites

  • A VPS account running Ubuntu 18.04 Operating System. Sign up with Digital Ocean and get free trial credit worth $100 to configure this article and test other cloud products.
  • A domain name (e.g. example.com). You can buy a domain name from a registrar of your choice(e.g. Namecheap).
  • A non-root user that can perform sudo tasks for your Ubuntu 18.04 Server.

Step 1: Point your Domain Name DNS records to your VPS

The first step is to point the DNS records of your domain name to your VPS . When visitors enter your domain name on a browser, they should be routed to the IP address associated with your VPS account.

You should ask your VPS provider about the correct DNS records to use if you are in doubt.

For instance, if you are running a VPS from Vultr, the DNS records look like these:

  • ns1.vultr.com
  • ns2.vultr.com

Step 2: Connecting to Your VPS Server

SSH to your Ubuntu 18.04 server using the public IP address (e.g. 198.18.0.22) associated with your VPS instance.  You can do this via the Command Line Interface (CLI) that ships with Linux or Mac. If you are running Windows on your local computer, consider downloading PuTTY SSH client.

Step 3: Installing Apache Web Server

The first software we are going to install is Apache. It’s an Open source web server application that runs the world’s busiest sites. Apache can handle large traffic and has lots of built-in security features and expandable modules.

To install Apache, first update the package information list on your Ubuntu server using the command below:

$ sudo apt-get update

Then, pull Apache from Ubuntu’s software repository using the command below:

$ sudo apt-get install apache2 php libapache2-mod-php

Press Y and hit Enter when prompted to confirm the installation.

Step 4: Initializing WordPress Website Directory Structure

Apache has a special feature called Virtual Hosts. This advanced feature allows you to host unlimited websites on a single Linux machine.

Before we create the Virtual Host configuration file, we must initialize the directory for holding our website files.

We have to create the directory under the /var/www/ folder using the command below:

$ sudo mkdir -p /var/www/example.com/public_html

Step 5: Creating a Virtual Host for the WordPress Website

When you install Apache, a default Virtual host (000-default.conf) is created under the /etc/apache2/sites-available directory. To keep things simple, we will create our own virtual host to run our WordPress site.

Please note, virtual hosts configuration files must end with a ‘.conf’ extension. So let’s create the configuration file for our domain name www.example.com

$ sudo nano /etc/apache2/sites-available/example.com.conf

Then, copy and paste the below content on that file:

<VirtualHost *:80>

    ServerAdmin admin@example.com

    ServerName example.com

    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

  <Directory /var/www/example.com/public_html>

     Options -Indexes +FollowSymLinks -MultiViews

     AllowOverride All

     Require all granted

  </Directory>

</VirtualHost>

Press CTRL+X, Y, and Enter to save the file.

We need to disable the default virtual host and enable the one we have just created, run the commands below:

$ sudo a2dissite 000-default.conf
$ sudo a2ensite example.com.conf

Restart Apache web server

$ sudo systemctl restart apache2

Step 6: Installing MySQL Database Server

WordPress relies on MySQL to store data. Apart from photos, plugins, and themes, post and web pages are stored in the MySQL relational database.

Like WordPress and Apache, MySQL is free and Open-source. It is also available on the Ubuntu’s software repository and we can install it using the apt command:

$ sudo apt-get install mysql-server

Press Y and hit Enter when prompted to confirm the installation

Step 7: Securing MySQL Server

MySQL installation is not secure by default. It comes with some test databases and other settings that we must disable. To do this, run the command below:

$ sudo mysql_secure_installation

You will get a prompt to answer multiple questions. We have prepared the correct responses, just follow the settings below:

Setup 'validate password' plugin? [Y/N] Y
Password Validation Policy Level: 2
Root Password: PASSWORD
Re-enter password: REPEAT PASSWORD
Continue with the password provided? Y
Remove anonymous users? [Y/N] Y
Disallow root login remotely? [Y/N] Y
Remove test database and access to it? [Y/N] Y
Reload privilege tables now? [Y/N] Y

If all goes well, you will get a success message.

Step 8: Creating a WordPress Database and User

Next, we are going to create a database and username for our WordPress software. First, log in to the MySQL command line interface using the command below:

$ sudo mysql -u root -p

Enter the root password of your MySQL server when prompted and hit Enter.

Then, on the command prompt that appears, enter the below SQL command to create a database:

mysql> Create database wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

To create a database user, run the command below:

mysql>Create user  'wp_user'@'localhost' IDENTIFIED BY 'PASSWORD';

Then, assign all privileges to the ‘wordpress’ database to the user we have created above using the command below:

mysql> Grant all privileges on wordpress.* to  'wp_user'@'localhost';

Flush privileges for the changes to take effect:

mysql> Flush privileges;

Exit from the MySQL command line interface:

mysql> Exit;

Step 9: Installing PHP on Ubuntu 18.04 Server

WordPress is written in PHP language and we must install the software on our Ubuntu 18.04 server in order for the application to function.

We are also going to install all the required PHP modules that WordPress require.

We can install all the software and modules in one line using the command below:

$ sudo apt-get install php php-cli php-common php-mbstring php-gd php-intl php-xml php-mysql php-zip php-curl php-xmlrpc

Restart Apache web server for the changes to take effect:

$ sudo systemctl restart apache2

Step 10: Downloading and Installing WordPress on Ubuntu 18.04 VPS

We now have a web server, a database for our WordPress software and PHP scripting language. We can now go ahead and download WordPress.

First, cd to the ‘tmp’ directory:

$ cd /tmp

Then, grab the latest WordPress version using wget command:

$ wget -c http://wordpress.org/latest.tar.gz

To unzip the WordPress installation files to the root directory of the virtual host that we created earlier, run the commands below:

$ sudo tar -xzvf latest.tar.gz
$ sudo cp -r wordpress/.  /var/www/example.com/public_html/

Apache runs under the www-data user, we need to grant the web server full permissions to control our website’s file. Run the command below:

$ sudo chown -R www-data:www-data /var/www/example.com/public_html

Then, we can set the appropriate permissions to ensure the outside world has read and execute permissions only:

$ sudo chmod -R 755 /var/www/example.com/public_html

To make sure that newly created files and folders inherit the correct permissions, we can run the command below:

$ sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \;

Step 11: Finalizing WordPress Installation on Ubuntu 18.04 Server

We now have WordPress installation files on the root of our website. However, we need to configure the database settings on the WordPress configuration files.

So type the command below to copy wp-config.php file from the default wp-config-sample.php file:

$ sudo cp /var/www/example.com/public_html/wp-config-sample.php /var/www/example.com/public_html/wp-config.php

Then, open the new configuration file that we have copied using a nano editor:

$ sudo nano /var/www/example.com/public_html/wp-config.php

Look for the values:

define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');

And change them to:

define('DB_NAME', 'wordpress');
/** MySQL database username */
define('DB_USER', 'wp_user');
/** MySQL database password */
define('DB_PASSWORD', 'PASSWORD');

Remember to replace PASSWORD with the correct value.

Before you close the file add the line below at the top:

<?php

And the line below at the bottom

?>

If you don’t add the two lines, PHP will not be able to parse the configuration file.

On a browser, enter your domain to finalize WordPress installation. If you followed all the steps, you should see the below web page:

Configuring WordPress with Apache, MySQL and PHP on Ubuntu 18.04 VPS

Congratulations! Your WordPress site is now installed. Just follow the wizard to complete the final settings.

Finally, you can create new posts, add a new web page or probably change the WordPress theme to match the look and feel of your company brand.

Conclusion

We have shown you how to install WordPress with Apache, MySQL, and PHP on Ubuntu 18.04 server.

Remember, you can run as many WordPress sites as required on your VPS plan by leveraging the power of Apache Virtual Host feature.

Sign up with Digital Ocean today and enjoy up to $100 worth of free trial credit to test this article and other cloud products.