Article

Install WordPress on LAMP with Ubuntu 20.04 LTS

Author: Francis Ndungu

Last Updated: Fri, Jun 4, 2021
CMS Ubuntu Web Servers

Introduction

WordPress is an open-source content management system that powers millions of personal blogs, corporate websites, and portals. Because it's written in PHP and MySQL/MariaDB, the WordPress installation steps can be completed by most developers in a few minutes. You can use the WordPress editor to fully customize your web pages without any HTML knowledge. In addition, WordPress allows you to take control of your website's look and feel by installing different themes and plugins.

This tutorial explains how to set up a WordPress site with a LAMP stack on your Ubuntu 20.04 LTS server at Vultr.

Prerequisites

To complete this tutorial, you need:

A domain name such as example.com is optional. You can use the public IP address of your server for testing purposes.

1. Install PHP Extensions and Enable mod_rewrite

  1. SSH to your server as a non-root user and update the package information index.

    $ sudo apt update -y
    
  2. Install the PHP extensions required by WordPress.

    $ sudo apt install -y php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
    
  3. Use the a2enmod command to enable Apache's mod_rewrite module. WordPress requires this to craft user-friendly URLs.

    $ sudo a2enmod rewrite
    
  4. Restart Apache to load the new changes.

    $ sudo systemctl restart apache2
    

2. Create Database and User Account

WordPress uses MySQL or MariaDB as the database server.

  1. Log in to your database server as root.

    $ sudo mysql -u root
    
  2. When prompted, enter your root password and press ENTER to proceed.

  3. Create a wordpress database.

    > CREATE DATABASE wordpress;
    
  4. Create a wp_user account for your WordPress database. Replace EXAMPLE_PASSWORD with a strong value.

    If you use MySQL, run these commands:

    > CREATE USER 'wp_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'EXAMPLE_PASSWORD';
    > GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
    > FLUSH PRIVILEGES;
    

    If you use MariaDB, use this command:

    > GRANT ALL PRIVILEGES on wordpress.* TO 'wp_user'@'localhost' identified by 'EXAMPLE_PASSWORD';
    
  5. Exit the database server.

    > quit;
    

Make note of these credentials, you'll need them later for the WordPress configuration file.

3. Create the WordPress Directory and Virtual Host File

  1. Create a wordpress directory under the root of your web server /var/www/.

    $ sudo mkdir /var/www/wordpress
    
  2. Disable the default virtual host file that ships with Apache.

    $ sudo a2dissite 000-default.conf
    
  3. Create a new configuration file using nano. This architecture separates the WordPress site configuration file to make troubleshooting easier in case a configuration problem occurs. This step is also mandatory if you intend to run multiple websites on your server.

    $ sudo nano /etc/apache2/sites-available/wordpress.conf
    
  4. Enter the information below into the file. Replace example.com with the domain name or public IP address of your server. The line AllowOverride All instructs Apache to allow the use of a .htaccess file in the /var/www/wordpress directory to override the web server's default configuration.

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/wordpress
        <Directory /var/www/wordpress>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
    </VirtualHost>
    
  5. Save and exit the file.

  6. Enable the configuration file.

    $ sudo a2ensite wordpress.conf
    
  7. Restart the web server to load the new changes.

    $ sudo systemctl restart apache2
    

3. Download WordPress

  1. Navigate to the tmp directory.

    $ cd /tmp
    
  2. Download the latest WordPress package.

    $ curl -O https://wordpress.org/latest.tar.gz
    
  3. Extract the archive.

    $ tar xzvf latest.tar.gz
    
  4. Copy the extracted files to the WordPress home folder.

    $ sudo rsync -rtv /tmp/wordpress/ /var/www/wordpress/
    
  5. By default, WordPress ships with a sample configuration file named wp-config-sample.php. Copy the sample to wp-config.php.

    $ sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
    
  6. Create an upgrade directory under the wp-content directory to allow WordPress to install upgrades without permission issues.

    $ sudo mkdir /var/www/wordpress/wp-content/upgrade
    
  7. Set the appropriate permissions for WordPress files.

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

5. Edit the WordPress Configuration

  1. For security, WordPress uses security keys and salts. To create a new set of secure values, use the WordPress secret key generator.

    $ curl -s https://api.wordpress.org/secret-key/1.1/salt/
    
  2. Copy the output to your clipboard.

  3. Edit your WordPress configuration file.

    $ sudo nano /var/www/wordpress/wp-config.php
    
  4. Locate the block below and replace it with the values from the WordPress secret key generator.

    ...
    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' );
    ...
    
  5. Locate these database configuration settings.

    /** The name of the database for WordPress */
    define( 'DB_NAME', 'database_name_here' );
    
    /** MySQL database username */
    define( 'DB_USER', 'username_here' );
    
    /** MySQL database password */
    define( 'DB_PASSWORD', 'password_here' );
    
  6. Replace the DB_NAME, DB_USER, and DB_PASSWORD values with the database details you created in section 2.

  7. Save and close the file.

6. Finish WordPress Setup

In a web browser, navigate to your server by name or IP address. Follow the on-screen prompts to complete the WordPress installation.

More Information

Want to contribute?

You could earn up to $300 by adding new articles