How to Install WordPress with Apache and Let's Encrypt SSL on Ubuntu 20.04

WordPress is a free, open-source and one of the most popular content management systems around the world. It is used for building a blog, a business website, or e-commerce on the internet. It is simple, easy to use, pluggable, customizable. It is written in PHP and uses MySQL/MariaDB as a database backend. According to the worldwide survey report, WordPress was used by more than 23.3% of the top 10 million websites.

In this post, we will walk you through the step by step process of installing WordPress with Apache and Let's Encrypt SSL on Ubuntu 20.04.

Requirements

  • A server running Ubuntu 20.04 server operating system.

  • A valid domain name pointed with your server IP.

  • A root password is set up on your server.

Install Apache Web Server

Before starting, Apache or Nginx web server must be installed on your server. In this guide, we will use the Apache webserver to host the WordPress site.

Run the following command to install the Apache webserver package:

apt-get install apache2 -y

Once the Apache has been installed, start the Apache service and enable it to start at system reboot:

systemctl start apache2 systemctl enable apache2

Now, open your web browser and type the URL http://your-server-ip to verify the Apache installation. If everything is fine, you should see the Apache test page:

Install PHP

WordPress is written in PHP language so PHP must be installed in your system. You can install PHP with other required extensions using the following command:

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

Once PHP is installed, verify the PHP version using the following command:

php -v

You should see the following output:

PHP 7.4.3 (cli) (built: Oct 6 2020 15:47:56) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Install and Configure MariaDB Database

WordPress uses MariaDB or MySQL to store their data So you will need to install MariaDB or MySQL in your server. You can install the MariaDB database server using the following command:

apt-get install mariadb-server -y

Once the MariaDB is installed, login to the MariaDB console with the following command:

mysql

Once you are login, create a database and user for WordPress with the following command:

MariaDB [(none)]> CREATE DATABASE wpdb; MariaDB [(none)]> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'dbpassword';

Next, flush the privileges and exit from the MariaDB console with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> EXIT

Download WordPress

Next, you will need to download the WordPress from their official website. First, change the directory to the Apache default root directory with the following command:

cd /var/www/html

Next, download the latest version of WordPress using the following command:

wget http://wordpress.org/latest.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf latest.tar.gz

Next, change the directory to the extracted directory and rename the wordpress sample config file:

cd wordpress mv wp-config-sample.php wp-config.php

Next, edit the config file and define your database information:

nano wp-config.php

Change the following lines based on your database settings:

define( 'DB_NAME', 'wpdb' ); /** MySQL database username */ define( 'DB_USER', 'wpuser' ); /** MySQL database password */ define( 'DB_PASSWORD', 'dbpassword' ); /** MySQL hostname */ define( 'DB_HOST', 'localhost' ); /** Database Charset to use in creating database tables. */ define( 'DB_CHARSET', 'utf8' );

Save and close the file then set proper permission and ownership to the wordpress directory:

chown -R www-data:www-data /var/www/html/wordpress chmod -R 755 /var/www/html/wordpress

Configure Apache to Host WordPress

Next, you will need to create an Apache virtual host configuration file to host the WordPress site.

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

Add the following lines:

<VirtualHost *:80> ServerName wordpress.example.com ServerAdmin webmaster@example.com DocumentRoot /var/www/html/wordpress ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Save and close the file then verify the Apache configuration file for any syntax error:

apache2ctl -t

You should get the following output:

Syntax OK

Next, activate the WordPress virtual host and restart the Apache service with the following command:

a2ensite wordpress.conf systemctl restart apache2

Access WordPress Installation Wizard

Now, open your web browser and access the WordPress installation wizard using the URL http://wordpress.example.com. You should see the following page:

Select your Language and click on the Continue button. You should see the following page:

Provide your WordPress site name, admin username, password, email then click on the Install WordPress button. Once the installation has been finished, you should see the following page:

Click on the Login button. You will be redirected to the WordPress login page:

Provide your admin username, password and click on the Log In button. You should see the WordPress dashboard in the following page:

Secure WordPress with Let's Encrypt SSL

It is always a good idea to secure your website with Let's Encrypt SSL. First, you will need to install the Certbot client to install and manage the SSL. You can install it with the following command:

apt-get install python3-certbot-apache -y

Once the Certbot is installed, run the following command to secure your website with Let's Encrypt SSL:

certbot --apache -d wordpress.example.com

You will be asked to provide your email and accept the term of service as shown below:

Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator standalone, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): hitjethva@gmail.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Plugins selected: Authenticator apache, Installer apache Obtaining a new certificate Performing the following challenges: http-01 challenge for wordpress.example.com Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/wordpress-le-ssl.conf Enabled Apache socache_shmcb module Enabled Apache ssl module Deploying Certificate to VirtualHost /etc/apache2/sites-available/wordpress-le-ssl.conf Enabling available site: /etc/apache2/sites-available/wordpress-le-ssl.conf

Next, select whether or not to redirect HTTP traffic to HTTPS as shown below:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and hit Enter to install the Let's Encrypt SSL for your website:

Enabled Apache rewrite module Redirecting vhost in /etc/apache2/sites-enabled/wordpress.conf to ssl vhost in /etc/apache2/sites-available/wordpress-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://wordpress.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=wordpress.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/wordpress.example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/wordpress.example.com/privkey.pem Your cert will expire on 2021-02-23. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

Now, you can access your WordPress website securely using the URL https://wordpress.example.com.

Conclusion

As you can see, we have successfully installed WordPress with Apache and Let's Encrypt SSL on Ubuntu 20.04 server. I hope this guide will help you to deploy the WordPress site easily on the server.


Was this page helpful?

Thank you for helping us improve!