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.

How to Setup a Web Server with LAMP Stack on Ubuntu 18.04 VPS

LAMP stack is a group of four Open-source software applications that are used together to run a full-blown web server. These include: Linux, Apache, MySQL and PHP.

Most of the world’s busiest sites run on Linux OS. Apache, on the other hand, is the cornerstone of highly trafficked websites and is one of the most used software in the internet economy.

The high-quality web server is fast, reliable and compatible with Linux.

MySQL is a Relational Database Management System (RDMS)that powers web applications by providing a secure environment for data storage.

PHP (Hypertext Preprocessor) is a general-purpose scripting language used to code dynamic websites.

PHP can be embedded in HTML and is executed on the server, therefore, offering great security for web applications.

This is a step-by-step guide for installing Linux, Apache, MySQL, and PHP on your Ubuntu 18.04 VPS.

At the end of the guide, you should be able to run a website on your Virtual Private Server.

Prerequisite

Step 1: Login to your VPS account

First, log in to your VPS account by entering the login credentials assigned to you when you created an account.

Next, create a VPS instance and select Ubuntu 18.04 as the Operating System. You will also see an option for selecting the server location(data center), vCPUs, RAM, Disk space and bandwidth.

If you are running just a few websites, a VPS plan with the below features should work pretty well:

  • 1 GB RAM
  • 1 vCPU
  • 25 GB disk space
  • 1 TB data transfer.

Next, SSH to VPS server using the public IP address of your instance, username (usually root) and password that was assigned to you.

If you are running Linux or Mac OS on your local computer, use the built-in Command Line Interface(CLI) to connect to your VPS server using the syntax below:

$ ssh root@198.18.0.12

Replace 198.18.0.12 with the public IP address associated with your VPS instance.

If you are on Windows, you can connect to your server through PuTTY SSH client.

Step 3: Creating a Non-root User with sudo Privileges

For security purposes, it is advisable to create a non-root user that can perform sudo tasks when your first set up a Linux server. You can then elevate privileges when running super admin tasks using the sudo command.

To create the user, run the command below and replace james with your desired name:

$ adduser james

You will be prompted to create a password for the new user. Next, press Enter multiple times to skip the other optional fields. Towards the end of the questions, you will be asked to confirm if the details for the new user are correct. Hit Y and press Enter to proceed.

Next, add the user to the sudoers group by running the command below:

$ adduser james sudo

You can now log out and login with the new user in order to install LAMP stack on your Ubuntu 18.04 server.

Step 4: Updating the Software Index

Ubuntu 18.04 has a nice command line package manager (apt). You can use it to install software applications as required.

The package manager maintains an index of software on the server.

First, update the package list information by running the command below:

$ sudo apt-get update

Step 5: Installing Apache Web Server on Ubuntu 18.04

We now have a VPS running Ubuntu 18.04 as the Operating System. We can now go ahead and install our first software – Apache. To do this,  run the command below:

$ sudo apt-get install apache2

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

Then, enter the public IP address associated with your server on a web browser to test Apache installation.

http://198.18.0.12

If the software was installed successfully, you should see the below web page:

The default website root directory on Apache is /var/www/html. So if you want to publish your website, you should upload all your files to that directory via an FTP client like Filezilla.

Step 6: Installing MySQL Server on Ubuntu 18.04

Next, we can install the MySQL database server on our VPS instance by running the command below:

$ sudo apt-get install mysql-server

Just before the installation is finalized, you will be prompted to enter a root password for your MySQL server.

You should not confuse this with the root password for your Ubuntu 18.04 server. Also, select a strong password with a mix of both letters and numbers. Also, ensure a good length of at least 8 characters.

MySQL server should be installed without any problems. To confirm if the installation was completed successfully, run the command below:

$ sudo service mysql status

Output

MySQL Server Status on Ubuntu 18.04 VPS

The default MySQL server installation is not secure. Luckily we can use the mysql_secure_installation command to harden the installation. Just run the command below:

$ sudo mysql_secure_installation

You will be taken through a series of questions. Enter the  answers below and hit Enter after each response:

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

MySQL server will be secured and you will get a success message at the end.

Step 6: Installing PHP Scripting Language on Ubuntu 18.04

PHP works together with a web server to deliver web content to a client. Once you request a PHP file, the PHP software processes the request and sends HTML back to your browser via a web server and in our case Apache.

To install PHP, run the command below:

$ sudo apt-get install php

Then, reload Apache web server:

$ sudo service apache2 restart

To test if PHP is working, create a file on the root of your website using nano text editor:

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

Then copy paste the below content:

<?php

phpinfo();

?>

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

Then, on a web browser visit the address below and replace the IP address with the public IP address associated with your Ubuntu 18.04 VPS

http:// 198.18.0.12/info.php

If you see the below web page, it means PHP is working as expected.

PHP info page on Ubuntu 18.04 VPS

PHP does not install all modules that are required for running a full-blown website by default. Luckily, we can install all these packages with a single command:

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

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

Then, restart Apache using the command below:

$ sudo service apache2 restart

Conclusion

In this guide, we have taken you through the steps of installing Linux, Apache, MySQL, and PHP in order to run and host a website from your VPS server.

If you are new to VPS hosting, sign up with Digital Ocean today and enjoy $100 free test credit.