How to Install WordPress with LAMP on Ubuntu 20.04
Introduction
WordPress is undoubtedly the most popular Content Management System. You can host about anything on WordPress- from simple portfolio websites, company landing pages, and blogs to full-fledged eCommerce websites. It guarantees flexibility, robustness, and security, which are key for the success of any website. Downloading and installing WordPress is straightforward. Then, once you have installed it on your server, most of the administration tasks can be done from the web frontend. WordPress requires a combination of software components to run. These software components usually come together to form a stack. In this tutorial, we will be focusing on using the LAMP stack to run our WordPress site.
LAMP stack is a very popular stack in the web development industry. Its popularity can be attributed to Apache, which is currently the most widely used web server in the world, PHP which is an open-source programming language with heavy support from the open-source community, and MySQL, a high-performance database management system guaranteeing concurrent high read-write speeds.
Let’s get started!
Prerequisites
In order to complete this tutorial, you’ll need access to an Ubuntu 20.04 server. To successfully install WordPress with LEMP on your server, you’ll also need to perform the following tasks before starting this tutorial:
- Create a
sudo
user on your server: The steps in this tutorial are using a non-root user withsudo
privileges. - Install a LAMP stack: WordPress will need a web server, a database, and PHP in order to correctly function. Setting up a LEMP stack (Linux, Nginx, MySQL, and PHP) fulfills all of these requirements. Follow this tutorial to install and configure this software.
Step 1: Set up a New MySQL Database and User for WordPress.
The very first step in installing a WordPress site is to create a database that WordPress will use to store and manage the site and user data. For brevity, we won’t go through the steps of installing and setting up the MySQL software itself since we have that in the LAMP stack tutorial. However, we need a database and a user that WordPress will use to login to the database. Login to the MySQL root account by entering the following on your terminal:
mysql -u root -p
Next, enter the following SQL statement in your terminal to create the database. Do not forget the semicolon at the end of the SQL statement:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
ALTER USER 'wordpressuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
EXIT;
Step 2: Configuring PHP to Work with WordPress Plugins
Of course, with the LAMP stack installation, you already have PHP installed, with some of the basic extensions necessary to serve a web page and communicate with MySQL. However, WordPress needs additional PHP extensions for its plugins to work. First, let’s update and install the necessary extensions using the following commands:
sudo apt update sudo apt install php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
For the extensions to start working, you must restart the Apache server. Enter the following command:
sudo systemctl restart apache2
Step 3: Configuring Apache’s .htaccess to Handle Override and Rewrite Rules
The .htaccess files sit in the root directory of a website. They contain rules that Apache uses to direct requests appropriately. WordPress uses its .htaccess to manipulate how Apache serves files from its root directory and subsequent subdirectories. Virtual hosts can help you host unlimited websites on one server. The virtual hosts config files are stored in the /etc/apache2/sites-available/ directory. The directory already has the 000-default.conf that comes with the Apache installation. It’s fine to use this default configuration file if you are only hosting one site on this server. In this tutorial, we are only hosting one site, so we can comfortably use this file.
Moreover, Ubuntu only allows access through the web to files stored in the /var/www directory. If you have a fresh LAMP installation, the default Apache welcome page appears when you navigate to your site’s public IP address. The welcome page index file is in the /var/www/html directory. We will be replacing this file with the WordPress files:
Enable .htaccess Overrides.
By default, the .htaccess directory override is disabled. For WordPress and its plugins to work, you must enable directory overrides. This is usually done in the virtual host files. To open the 000-default.conf file using nano editor, enter the following command:
sudo nano /etc/apache2/sites-available/000-default.conf
The AllowOverride directive is added within the Directory block pointing to the website’s document root. Add the directory block and update the document root. The configuration file should now look like this:
|
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html>
AllowOverride All
</Directory>
</VirtualHost>
|
To save the file, press Ctrl + O, ENTER. Press Ctrl + X to exit the editor.
Enable the Rewrite Module.
WordPress comes with a permalink feature which ensures beautiful search engine optimized URLs. They depend on an Apache module called mod_rewrite. You can enable the module using the following command:
sudo a2enmod rewrite
Test the Configuration Changes.
To be sure that everything will work fine after the changes you made, enter the following command to test the changes:
sudo apache2ctl configtest
If you see Syntax ok, then we are good to proceed. If you see something else, you will have to go back and correct any typos you may have left in the configuration files.
Enable the Changes.
To enable the new changes, enter the following command to restart Apache:
sudo systemctl restart apache2
Up to this point, our server is ready to serve WordPress.
Step 4: Downloading WordPress
We can now download WordPress and set up our site. The document root for our site will be in the /var/www/html directory. We will replace the default Apache welcome page. First, we need to download WordPress from their site. For security concerns and other changes, always download the latest version. Enter the following command to change into the writable temporary directory:
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
touch /tmp/wordpress/.htaccess
cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
mkdir /tmp/wordpress/wp-content/upgrade
Since we are using the default Apache webroot, it probably has the Apache welcome page index.html file. Remove it by entering the following command:
sudo rm index.html
sudo cp -a /tmp/wordpress/. /var/www/html
Step 5: Setting Up User Permissions and Database Credentials in the WordPress Directory
Before accessing the web front-end administration panel, you must adjust a few settings. Those include the directory ownership permissions, and database user credentials.
Adjust Directory Ownership and Permissions.
The ownership of the WordPress directory currently belongs to the sudo user. The ownership needs to be changed to the www-data user and group, which is what the Apache web server uses. Changing ownership to www-data allows WordPress to read and write files, hence properly serving the web pages and performing automatic updates when necessary. We can update the ownership using the chown command. Enter the following on your terminal:
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html/ -type d -exec chmod 750 {} \; sudo find /var/www/html/ -type f -exec chmod 640 {} \;
Update the WordPress Configuration File
The file we will be editing in this section is the wp-config.php, which contains the necessary configurations that run the WordPress site. First, let’s change the secret keys to improve the security of the installation. Instead of having to generate your own keys, WordPress offers a secret key generator utility that you can use to get some highly secure keys. You can get them using the following command:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
The output shown above contains the exact configuration lines that you can copy and paste in the wp-config.php file. Highlight the keys you received and copy them to your clipboard. Open the wp-config.php file in the nano editor using the following command:
sudo nano /var/www/html/wp-config.php
Delete these dummy strings and replace them with the ones you copied from the command line. Next, WordPress needs to communicate with the database that you had created in Step One of this tutorial. At the beginning of the config file, you should see the MySQL settings section. It will be something like this:
You need to update the database name, database user, and the associated password as you had created them in Step one. Lastly, you need to define the method that WordPress will use to write to the file system. Since the Apache server has the right permissions to access all the directories in the WordPress installation directory, we can set the file system access method to direct. This is also defined inside the wp-config.php file.
You can simply define the string below the database settings section:
define('FS_METHOD', 'direct');
Save and close the wp-config.php file when you are ready.
Step 6: Finalize the Installation through the Web Frontend
At this point, the server’s configuration is complete and you should be able to access your WordPress installation directly on the web through your domain name ( http://greencloud.com/wp-admin/install.php) or server’s public IP address. You should see the WordPress initial setup page. Select your language and proceed:
Next, you will be presented with the main setup page, where you set the site’s title, username, and password that you will be using to log in to the admin dashboard. Fill in the details appropriately. A strong password is already generated for you. You may optionally type in a strong password of your choice. Save and continue:
Currently, WordPress installs successfully, and the next page prompts you to login:
Once you log in, you will be directed to the WordPress administration dashboard:
Your homepage, with the default WordPress blog post now looks like this:
Conclusion
In this tutorial, you successfully configured a LAMP stack and installed a WordPress site. This is a huge step because you can now host any WordPress site in just a few minutes. The next steps include looking at WordPress themes that may improve the appearance of your site, WordPress plugins to add functionalities, or trying out a bunch of other stuff you can do with WordPress.