How to Install WordPress: Ubuntu 18.04 Using LAMP Stack

How to Install WordPress: Ubuntu 18.04 Using LAMP Stack

WordPress is the most popular Content Management System (CMS) thanks to its user-friendliness and flexibility to create all manner of websites. The software can also be installed on different types of hosting, including a Virtual Private Server (VPS).

To install WordPress on your server, you can use the LAMP (Linux, Apache, MySQL, and PHP) stack. This method is ideal for when you need complete control over the WordPress back-end.

This article will cover the details of the WordPress installation on Ubuntu 18.04 using the LAMP stack, from installing the Apache server to configuring WordPress via a web browser.

Before we get started, you’ll need to access the VPS using an SSH client. Check our PuTTY tutorial on how to do that.

Pro Tip

Make sure you use a root or sudo user since this method involves a few installations and configuring the firewall.

Step 1. Install and Configure the Apache Web Server

The first step to set up the LAMP stack is to install and configure the Apache server. First, we have to update and upgrade the package list on your system and upgrade the packages to the newest version. Do so by using these commands on your SSH client:

sudo apt update -y
sudo apt upgrade -y

If you’re asked to enter a password, input your VPS root password and press Enter.

Now it’s time to install the Apache2 web server on your VPS. If you buy a VPS plan from Hostinger, it comes with Apache2 pre-installed. If you follow the next step, it does not harm your VPS, but you can skip it.

Run the following command to install Apache2:

sudo apt install apache2

Hostinger’s VPS doesn’t come with The Uncomplicated Firewall (UFW) pre-installed. However, if you’ve installed UFW on your VPS, 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

If you run that command on a default Hostinger VPS that doesn’t have UFW, it should print the following output:

-bash: ufw: command not found

Step 2. Install PHP

PHP is necessary for WordPress to communicate with the MySQL database and display dynamic content. You will also need to install additional PHP extensions for WordPress.

Run the following command to install PHP and PHP extensions at once:

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl -y

When you request a directory without specifying a filename, index.html will be given the priority and hence will be displayed. You can change the priority order in the dir.conf file. Use the following command to open it using Nano text editor:

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

You should see the following:

The configuration to open the dir.conf file using the Nano text editor.

When loading the website, it will resolve the files from left to right. You have to place all files in the correct order of priority. For example, if you want index.php to have a higher priority than index.html, simply move index.php to the left of index.html.

Once you’ve changed the file, save it and exit by pressing CTRL+X. Then, type Y to save changes and Enter to close it.

Now you have to restart the Apache2 web server so that the changes take place. Run the following command to do so:

sudo systemctl restart apache2

Create a sample PHP file in the web root directory to check whether PHP works. Use this command to go to the directory:

cd /var/www/html

Then use this command to create a sample PHP file and open it using the Nano text editor:

nano sample.php

In the text editor, insert the following code:

<?php
phpinfo();
?>

Save and exit the file.

Now, access the file by entering http://your-IP-adress/sample.php in your web browser. You should see this PHP info page:

PHP info page.

Pro Tip

You can find your IP address in your Hostinger hPanel under the VPS SSH details.

VPS SSH Details in hPanel

We recommend removing the file after checking the installation. The PHP info displays PHP installation and server configuration, which may help cyber attackers to access your server. Use this command to delete it:

sudo rm /var/www/html/sample.php

Step 3. Configure MySQL and Create a Database

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

apt install mysql-server -y

It’ll be necessary to enter your password. To complete the installation, press Y and Enter when prompted.

After installing MySQL on your VPS, open the MySQL terminal by typing the following command:

sudo mysql

Set the password for the MySQL root account using this command:

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

Make sure to enter a strong MySQL root password in place of YOURPASSWORD.

To implement these changes, run the flush command:

mysql> FLUSH PRIVILEGES;

Use the following command to create a WordPress database:

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 WordPress database. We’ll use WordPressDB as the database name and testhostinger as the username:

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 this command:

mysql> EXIT;

Step 4. Prepare to Install WordPress on Ubuntu

It’s time to prepare the WordPress installation by creating a WordPress configuration file and a WordPress directory.

Creating a WordPress.conf File

Start by creating a WordPress.conf Apache configuration file in the /etc/apache2/sites-available directory. Use the following command:

nano /etc/apache2/sites-available/WordPress.conf

Keep in mind that file and locale names are case-sensitive on Linux.

Once you run that command, you’ll get to the Nano text editor to edit the WordPress.conf file. Enable .htaccess by adding these lines to the VirtualHost block:

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

Close and save the file by pressing CTRL+X. Press Y and Enter when prompted.

Creating a WordPress Directory

Then, create a directory for WordPress in /var/www/. In our example, its full path will be /var/www/wordpress. To do so, use the mkdir command to create the directory:

mkdir /var/www/wordpress

Now, enable mod_rewrite to use the WordPress permalink feature by running the following command in the terminal:

sudo a2enmod rewrite

You’ll have to restart the Apache web server by using the following command:

systemctl restart apache2

The next step is changing the ServerName directive in the /etc/apache2/apache2.conf file. Open the file using this command:

nano /etc/apache2/apache2.conf

You’ll have to configure the ServerName directive to the server’s IP address or hostname by adding the following line to the /etc/apache2/apache2.conf file:

ServerName <Your IP Address>

Close and save the file.

Now, you have to check whether the Apache configuration is correct by running the following command on the terminal:

apachectl configtest

If the configuration works fine, it should print the following output:

Syntax OK
Output: Syntax OK window

Step 5. Download and Configure WordPress

After all the preparations are complete, it’s time to install WordPress. There are two methods – setting up WordPress via a web interface or manually editing the wp-config.php file.

Method 1. Configuring WordPress via a Browser

First, install the wget package on your VPS. This will be useful for downloading WordPress files. Run this command on the command line:

sudo apt install wget -y

Then, use the wget command followed by the WordPress download link:

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

Once you have downloaded the archive file, install the unzip utility using these commands:

ls
sudo apt install unzip -y

Now you will have to move the file to the correct directory before unzipping it. Use the command:

mv latest.zip /var/www/html

Then, navigate to the directory and unzip the file using these commands:

cd /var/www/html
unzip latest.zip

After that, use the following command to move the directory:

mv -f wordpress/* ./

The last step is to remove index.html. Use the following command:

sudo rm -rf index.html

You can use the ls command to verify if the index.html file has been removed. Once that’s done, restart Apache by using these commands:

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

Finish it by setting up WordPress via a web browser. Open a web browser and type in the server’s IP address. The following steps will be similar to a standard WordPress setup.

First, select a language for WordPress and click Continue.

WordPress setup - choosing the language.

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 - highlighting the "Let's go" button

It will take you to the main setup page. Fill in the following details:

  • Database name – enter the name you set up when configuring the WordPress database. In this case, it will be WordPressDB.
  • Username – type in the MySQL username you’ve set up for the database earlier.
  • Password – input the password you’ve created for the database user.
  • Database host – keep the default value localhost here.
  • Table prefix – leave wp_ in this field.

Click Submit to continue.

WordPress setup page, showing the form for database connection details

A new message will appear saying that WordPress can now communicate with your database. Click Run the installation.

WordPress message informing that database connection is successful - highlighting the "Run the installation" button

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 its 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 it.

WordPress setup page - installation form

A success message will appear along with a login button. You can access WordPress straight from this page.

Success message: WordPress has been installed

Once you have logged in, you’ll be taken to the WordPress administration dashboard. Now you can start customizing the website by installing WordPress plugins and themes.

If your WordPress site does not have a domain name yet, purchase one and point the domain name to the VPS before making the website public.

Method 2. Manually Editing the wp-config.php File

Alternatively, install WordPress by manually editing the wp-config.php file. Use these commands to change your current working directory and download the WordPress archive file:

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

Then, run the following command to extract the archive file:

tar xzvf latest.tar.gz

Create a .htaccess file in the /tmp directory by using this command:

nano /tmp/wordpress/.htaccess

Save the file by pressing CTRL+X and then Y and Enter when prompted.

Now, you have to rename the WordPress sample configuration file. By default, it’s named wp-config-sample.php. Rename it by using this command:

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

Create an update folder in the /var/www/html path so WordPress doesn’t run into permission issues in the future:

cd /var/www/html
mkdir wp-content/update                                   

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

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

Change the ownership of WordPress files to the www-data users and groups as the Apache web server will use them. To change the ownership, run this command:

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

Then, set the correct permissions for 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 runs. Copy the output and replace the dummy values in the wp-config.php file. Type in this command to open and edit the file:

nano /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 the 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.

Conclusion

WordPress is a popular CMS 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 back-end.

Remember to use sudo or root user as the installation process requires administrative access. Let’s recap the steps to install WordPress CMS on a server running on Ubuntu 18.04:

  • Install Apache2 – it will be the basis for your web server.
  • Install PHP – WordPress will use it to communicate with the database. Remember to install the PHP extensions, too.
  • Set up MySQL – it’ll act as the database for all WordPress files.
  • Prepare for WordPress installation – a WordPress directory and the WordPress.conf are required for the installation process.
  • Download and install WordPress on Ubuntu – finish the process by setting up the WordPress site.

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

Leonardus N.

Leo is a Digital Content Writer at Hostinger. He loves to share his web hosting and WordPress knowledge to help people build a successful online presence. During his free time, he likes to play music and learn audio engineering.