How to Install WordPress on Ubuntu 18.04 Using LAMP Stack

How to Install WordPress on Ubuntu 18.04 Using LAMP Stack

If you have access to a Virtual Private Server (VPS), you can host your very own WordPress website on it. WordPress is a free and open-source Content Management System (CMS), powering over 42% of all websites.

If your VPS runs on Ubuntu 18.04, you can install WordPress on it by using the LAMP (Linux, Apache, MySQL, and PHP) stack to fully control the backend. This tutorial will cover the installation process in detail, from setting up Apache to configuring WordPress via a web browser.

Remember, you’ll need to access your VPS using SSH to install WordPress on Ubuntu. Check out our PuTTY tutorial on how to do that.

Pro Tip

Make sure you have the required sudo access since this method involves a few installations and a firewall configuration.

Install and Configure Apache2

The first step to set up the LAMP stack is to install and configure the Apache2 web server. Start by opening the Terminal application on Ubuntu.

Ubuntu desktop, highlighting the terminal app

Then, enter this command:

$ sudo apt update -y

If you’re asked to enter a password, input your Ubuntu password and press Enter. Note that it won’t be shown as you type it.

Entering the password via the terminal

Then, run the following command to install the Apache2 web server on Ubuntu:

$ sudo apt install apache2

If you use the Uncomplicated Firewall (UFW), it may restrict Apache’s HTTP and HTTPS traffic. To check your UFW application profiles, enter this command:

$ sudo ufw app list

The output will look like this:

Available applications:
 Apache
 Apache Full
 Apache Secure
 OpenSSH

Install MySQL

Once Apache is running, the next step is to install the MySQL database. To do so, run the following command on Terminal:

$ sudo apt install mysql-client-core-5.7

You’ll be prompted to enter your password. To complete the installation, press Y and Enter when prompted. Open the MySQL terminal by typing:

$ sudo mysql

Set the password for the root user using this command:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH
mysql_native_password BY ‘YOURPASSWORD;

To implement these changes, run the Flush command:

mysql> FLUSH PRIVILEGES;

Type exit to close the MySQL command prompt and complete the installation.

Install PHP

PHP is necessary for WordPress to communicate with the MySQL database and display dynamic content. To install it, run this command:

$ sudo apt install php libapache2-mod-php php-mysql

Then, install additional PHP extensions for WordPress by entering this command:

$ sudo apt install php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl

When you request a directory without specifying a filename, index.html will be displayed by default. If you want index.php to be loaded instead, run this command:

$ sudo nano /etc/apache2/mods-enabled/dir.conf

You should see the following:

Inserting the command to load index.php via the terminal

Replace index.html with index.php and move index.cgi further to the right. Save the file to continue.

Implement the changes by restarting the Apache server:

$ sudo systemctl restart apache2

To check whether PHP works, create a sample PHP file, name it sample.php and add the following code to it:

<?php

phpinfo();

?>

Add this file to /var/www/html/, the web root directory of Apache. Access the file by entering http://your_public_IP/Sample.php into your web browser.

MySQL Setup for WordPress

Next, we’ll create a WordPress database and set up a new user account for it. This will make managing the site easier and improve its security.

On Terminal, log in to the MySQL root account by entering:

$ sudo mysql -u root -p

You will be prompted to input the MySQL root password that we set up earlier. To create a separate database that WordPress will control, input:

mysql> CREATE DATABASE WordPressDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Now, we’ll create a MySQL user account to operate on the new database. We’ll use testhostinger as the username:

mysql> GRANT ALL ON WordPressDB.* TO 'testhostinger'@'localhost' IDENTIFIED BY 'newpassword;

Make sure to enter a strong password in place of newpassword.

Once done, flush the privileges so that MySQL implements the changes:

mysql> FLUSH PRIVILEGES;

Finally, exit MySQL by typing:

mysql> EXIT;

Prepare to Install WordPress on Ubuntu

With the new MySQL database and its user created, it’s time to get ready to install WordPress.

Start by creating an Apache configuration file WordPress.conf in the /etc/apache2/sites-available/ directory. Keep in mind that file and locale names are case-sensitive on Linux.

Then, create a directory for WordPress in /var/www/. In our example, its full path will be /var/www/wordpress.

Now, open WordPress.conf and enable .htaccess in it by adding these lines to the VirtualHost block:

<Directory /var/www/wordpress/>
   AllowOverride All
</Directory>

Save the file.

Next, enable mod_rewrite to use the WordPress permalink feature by running the following command in Terminal:

$ sudo a2enmod rewrite

In the file /etc/apache2/apache2.conf, change the ServerName directive to the server’s IP address or hostname. If you do not know your IP address, you can view it on Terminal by running:

$ ip a

You will see your IP address right next to inet.

To prepare for the WordPress installation, head to the WordPress download page and copy the link from the Download WordPress 5.7.1 button.

Copying link address on a WordPress page

Configure and Install WordPress on Ubuntu

Head back to Ubuntu’s Terminal and type in this command:

$ sudo apt install wget -y

Then, paste the WordPress download link:

$ wget https://wordpress.org/latest.zip

Once you have installed the document, you will need to unzip the file. Run these commands:

$ ls
$ sudo apt install unzip -y
$ unzip latest.zip

After that, copy all of the files to the web root directory:

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

Go to /var/www/html by typing:

$ cd /var/www/html
$ ls

The last step is to remove index.html:

$ sudo rm -rf index.html
$ ls

Once that’s done, restart Apache2:

$ sudo systemctl restart apache2
$ sudo chown -R www-data:www-data /var/www/

There’s an alternative way to install WordPress. Begin by changing your current location to a directory:

cd /tmp
curl -O https://wordpress.org/latest.tar.gz

Extract this file using:

tar xzvf latest.tar.gz

Here, create the .htaccess file:

vi /tmp/wordpress/.htaccess

Save the file. Then, rename the wp-config-sample.php file using the command:

mv /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php

Create the update folder so WordPress doesn’t run into permission issues in the future:

mkdir /tmp/wordpress/wp-content/upgrade

This command completes the initial setup. Now, we can copy the files into the document root directory:

$ sudo cp -a /tmp/wordpress/. /var/www/wordpress

To make sure everything works correctly, change the ownership of WordPress files to the www-data users and groups – the Apache web server will be using them.

To change the ownership, run this command:

$ sudo chown -R www-data:www-data /var/www/wordpress

Then, set the correct permissions to the directories and files:

$ sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
$ sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;

For the initial configuration, you’ll also need to generate the WordPress salts. Run this command to do this:

curl -s https://api.wordpress.org/secret-key/1.1/salt/

This command will produce unique salt values each time it’s run. Copy the output and replace the dummy values in the wp-config.php file. Type in this command to edit the file:

vi /var/www/wordpress/wp-config.php

The wp-config.php file also contains database configuration details at the top. Replace the DB_NAME, DB_USER, and DB_PASSWORD with values you have set for WordPress.

define('DB_NAME', 'WordPressDB');
/** MySQL database username */
define('DB_USER', 'WordPressUser');
/** MySQL database password */
define('DB_PASSWORD', 'DB_Password');

Finally, add the file system method at the very bottom:

define('FS_METHOD', 'direct');

Save the file after making the changes.

Complete the Setup via Your Web Browser

With the backend configuration out of the way, we’ll finish setting up WordPress via the web interface. Open a web browser and type in your server’s IP address.

The following steps will be similar to a standard WordPress setup:

  1. Select a language for WordPress and click Continue.
Selecting the language on WordPress setup wizard
  1. A Welcome to WordPress message will appear listing the information you’ll need to complete the setup. Click on the Let’s go! button to continue.
Welcome to WordPress message
  1. WordPress will display a form. Fill in the following details:
  • Database Name – enter the database name you set up when configuring MySQL.
  • Username – type in the username you set up for the database earlier.
  • Password – input the password you created for this user.
  • Database Host – keep the default value localhost here.
  • Table Prefix – leave wp_ in this field.

Click Submit to continue.

Filling out the database information on WordPress setup wizard
  1. A new message will appear saying that WordPress can now communicate with your database. Click Run the installation. After that, you’ll have to enter some more information:
  • Site Title – type in the WordPress website name. To optimize your site, we recommend inputting the domain name.
  • Username – create a new username you’ll use to log in to WordPress.
  • Password – create a password for the WordPress user.
  • Your Email – add the email address for updates and notifications.
  • Search engine visibility – leave this box unchecked if you don’t want search engines to index your site until it is ready.

Click the Install WordPress button to finish the installation.

Filling out the site information on WordPress setup wizard
  1. A Success! message along with the login form will appear. You can access WordPress straight from this page.
Logging into WordPress

Once you have logged in to your WordPress website, you can customize it by installing plugins and themes.

Note that your WordPress site does not have a domain name yet – get one before making the website public.

Conclusion

WordPress is a popular content management system that is excellent for website creation. If you have VPS hosting, setting up WordPress using the LAMP stack is a great way to power your site and access its backend.

Let’s recap the steps to install WordPress on a server running on Ubuntu 18.04:

  • Install Apache2 – it will be the basis for your web server.
  • Set up MySQL – it’ll act as the database for all WordPress files.
  • Install PHP – WordPress will use it to communicate with the database. Remember to install the PHP extensions, too.
  • Download and install WordPress – finish the process by setting up the WordPress CMS.

We hope this tutorial has taught you how to install and configure WordPress on Ubuntu. Go ahead and try it yourself. If you have any questions, leave them in the comment section below.

Author
The author

Astari Sarosa

Astari is a Digital Content Writer at Hostinger, who is passionate about WordPress, digital marketing, and website builders. She enjoys sharing her knowledge through her writings. During her free time, Astari likes to try new activities, go on adventures, and learn about different cultures.