DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on • Edited on

2 1

Install WordPress on Ubuntu with LAMP Stack

Prerequisites

Step 1 — Creating a MySQL Database and User for WordPress

To get started, log into the MySQL root (administrative) account by issuing the following command: sudo mysql -u root -p Within the database, create a dedicated database for WordPress to control. You can call this whatever you would like, but we will be using the name wordpress in this guide. Create the database for WordPress by running the following command:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Next, you’re going to create a separate MySQL user account that you’ll use exclusively to operate your new database. We will use the name wordpressuser in this guide by running the following command:

CREATE USER 'wordpressuser'@'%' IDENTIFIED WITH mysql_native_password BY 'your_password';

Next, let the database know that your wordpressuser should have complete access to the database you set up:

GRANT ALL ON wordpress.* TO 'wordpressuser'@'%';

You need to flush the privileges so that the current instance of MySQL knows about the recent changes made: FLUSH PRIVILEGES;

Step 2 — Installing Additional PHP Extensions

When setting up our LAMP stack, we only required a very minimal set of extensions in order to get PHP to communicate with MySQL. WordPress and many of its plugins, however, leverage additional PHP extensions. You can download and install some of the most popular PHP extensions for use with WordPress:

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

This will lay the groundwork for installing additional plugins on your WordPress site.

You’ll need to restart Apache to load these new extensions: sudo systemctl restart apache2

Step 3 — Adjusting Apache’s Configuration to Allow for .htaccess Overrides and Rewrites

In this guide, we’ll use /etc/apache2/sites-available/wordpress.conf as an example, but you should substitute the path to your existing configuration file where appropriate. Additionally, we will use /var/www/html/wordpress as the root directory of our WordPress installation.

Enabling .htaccess Overrides

Currently, the use of .htaccess files is disabled. WordPress and many WordPress plugins use these files extensively for in-directory tweaks to the web server’s behavior.

Open the Apache configuration file for your website with your preferred text editor. Here, we’ll use nano:

sudo nano /etc/apache2/sites-available/wordpress.conf

To allow .htaccess files, you need to set the AllowOverride directive within a Directory block pointing to your document root. Add the following content inside the VirtualHost block in your configuration file, making sure to use the correct web root directory:

<VirtualHost *:80>
  ServerAdmin webmaster@your-domain.com

  ServerName your-domain.com
  ServerAlias www.your-domain.com
  DocumentRoot /var/www/html/wordpress

  <Directory /var/www/html/wordpress/>
        Options FollowSymlinks
        AllowOverride All
        Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/your-domain.com_error.log
  CustomLog ${APACHE_LOG_DIR}/your-domain.com_access.log combined
</VirtualHost>

Save the file and Exit.

Enable the Wordpress virtual host: sudo a2ensite wordpress.conf

Enabling the Rewrite Module

Next, you can enable mod_rewrite so that you can use the WordPress permalink feature: sudo a2enmod rewrite

Step 4 — Downloading WordPress

First, change into a writable directory (we recommend a temporary one like /tmp): cd /tmp

Then download the compressed release with the following curl command: curl -O https://wordpress.org/latest.tar.gz

Extract the compressed file to create the WordPress directory structure: tar xzvf latest.tar.gz

You’ll be moving these files into your document root momentarily. Before doing so, you can add a dummy .htaccess file so that this will be available for WordPress to use later.

Create the file by running the following: touch /tmp/wordpress/.htaccess

You’ll also copy over the sample configuration file to the filename that WordPress reads:

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

Additionally, create the upgrade directory so that WordPress won’t run into permissions issues when trying to do this on its own following an update to its software: mkdir /tmp/wordpress/wp-content/upgrade

Now you can copy the entire contents of the directory into your document root:

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

Step 5 — Setting Up the WordPress Configuration File

To grab secure values from the WordPress secret key generator, run the following:

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

You will receive unique values that resemble output similar to the following:

Next, open the WordPress configuration file: sudo nano /var/www/html/wordpress/wp-config.php

Find the section that contains the example values for those settings:

Delete those lines and insert the values you copied from the command line:

You need to adjust the database name, the database user, and the associated password that you configured within MySQL.

Step 6 — Completing the Installation Through the Web Interface

In your web browser, navigate to your server’s domain name or public IP address:

When you click ahead, you will be taken to a page that prompts you to log in:

Once you log in, you will be taken to the WordPress administration dashboard:

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more