Install WordPress with Apache and Let’s Encrypt SSL on Ubuntu 22.04

0

WordPress is one of the most popular content management systems on the internet. It is open-source CMS and works well with almost any web hosting service, making it one of the easiest to install and use for building any type of website.

We recommend installing WordPress on VPS (virtual private server) over regular web hosting for traffic-intensive websites.

Here, we will see how to install WordPress with Apache on Ubuntu 22.04.

Set up LAMP Stack

Follow the below link to install the LAMP stack on your Debian system

Install AMP (Apache, MariaDB, and PHP) on Ubuntu 22.04

Set up PHP for WordPress

WordPress requires a few extensions for a WordPress installation. So, install them using the apt command.

sudo apt install -y php-curl php-xml php-common php-imagick php-json php-mbstring php-mysql php-zip php-bcmath php-gd

Additionally, you may need to change PHP values based on the requirement. You may start with the below values now and increase or decrease them later when required.

max_connection_time = 300

upload_max_filesize = 64M

post_max_size = 64M

sudo sed -i 's/^max_execution_time \= .*/max_execution_time \= 300/g' /etc/php/8.1/apache2/php.ini

sudo sed -i 's/^upload_max_filesize \= .*/upload_max_filesize \= 64M/g' /etc/php/8.1/apache2/php.ini

sudo sed -i 's/^post_max_size \= .*/post_max_size \= 64M/g' /etc/php/8.1/apache2/php.ini

Setup Apache Virtual Host

We will start with creating an Apache virtual host for a WordPress installation. Typically virtual host files contain a domain name, port number, document root, log location, fast CGI, etc.

Assume the following,

Domain name: www.itzgeek.net
Port No: 80
Document root: /var/www/html/www.itzgeek.net/

First, create a virtual host configuration file.

sudo nano /etc/apache2/sites-available/www.itzgeek.net.conf

Then, place the following virtual host information into the above configuration file. You will need to change ServerName and other values as per your requirement.

<VirtualHost *:80>

   ServerName itzgeek.net
   ServerAlias www.itzgeek.net
   ServerAdmin admin@itzgeek.net
   DocumentRoot /var/www/html/www.itzgeek.net


   ErrorLog ${APACHE_LOG_DIR}/www.itzgeek.net_error.log
   CustomLog ${APACHE_LOG_DIR}/www.itzgeek.net_access.log combined


   <Directory /var/www/html/www.itzgeek.net>
      Options FollowSymlinks
      AllowOverride All
      Require all granted
   </Directory>

</VirtualHost>

Next, create a document root directory to place WordPress files.

sudo mkdir -p /var/www/html/www.itzgeek.net/

Then, enable the virtual host and SSL, and rewrite modules.

sudo a2ensite www.itzgeek.net

sudo a2enmod rewrite ssl

Finally, restart the Apache service.

sudo systemctl restart apache2

If you do not plan to set up an SSL certificate for WordPress, then proceed to install WordPress directly.

Install Let’s Encrypt SSL Certificate

Create DNS Record

Visit your domain registrar’s DNS management page and create an A and CNAME (if you want to use www subdomain) record for your domain. Typically you will have to make two records for your WordPress website.

  1. Non-www Domain Name (Ex. itzgeek.net) >> A record point to your server IP
  2. www Domain Name (Ex. www.itzgeek.net) >> CNAME record point to itzgeek.net
DNS Records
DNS Records

Install Certbot client

The Certbot client, which helps us generate and install the Let’s Encrypt SSL certificate, is now available as a snap package for Ubuntu operating system. So, first, install snapd daemon on your system.

sudo apt update

sudo apt install -y snapd

Then, update snapd to the latest version.

sudo snap install core && sudo snap refresh core

Finally, install the Certbot client using the below command.

sudo snap install --classic certbot

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Install SSL Certificate

Use the certbot command to generate and install the Let’s Encrypt SSL certificate on the Apache webserver.

sudo certbot --apache
1. Enter email address to receive notification on urgent renewal and security notices
2. Type Y and press Enter to register with the ACME server
3. Type Y or N to receive emails about EFF news, campaigns, and newsletters.
4. Certbot will automatically detect the WordPress domain and ask you permission to activate HTTPS for your WordPress website. Type 1 or appropriate numbers separated by a comma if you have multiple websites.
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: itzgeek.net
2: www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1,2

Wait for the SSL installation to complete.

Redirect non-www HTTP requests to www HTTPS with Apache

The Certbot client will place the required rules to redirect the traffic from HTTP to the HTTPS website.

1. http://itzgeek.net >> https://itzgeek.net

2. http://www.itzgeek.net >> https://www.itzgeek.net

As you can see, the first domain is not reaching www HTTPS with rules placed by Certbot. So, you will have to add a rule manually to redirect traffic from non-www HTTP to www HTTPS domain if required, I.e., http://itzgeek.net >> https://www.itzgeek.net.

Auto-Renew SSL Certificate

The Certbot client comes with a systemd service that renews SSL certificates automatically. So, you will not have to renew the certificates manually.

Install WordPress with Apache on Ubuntu 22.04

Create WordPress Database

First, login into MariaDB/MySQL database server.

sudo mysql -u root -p

Then, create the database for WordPress installation and the database user and password.

CREATE DATABASE wpdatabase;

CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'wppassword';

GRANT ALL PRIVILEGES ON wpdatabase.* TO 'wpuser'@'localhost';

EXIT

Download WordPress

First, download the latest version of the WordPress files using the wget command.

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

Then, extract the downloaded file.

tar -zxvf latest.tar.gz

And then, move the files to your website document root directory.

sudo mv wordpress/* /var/www/html/www.itzgeek.net/

Next, update the ownership and a group of the WordPress directory.

sudo chown -R www-data:www-data /var/www/html/www.itzgeek.net/

Install WordPress

Open your browser and visit your WordPress website domain to perform the WordPress installation.

https://your-wordpress-website

1. Choose the Installation Language and click Continue

2. Click Let’s go!

3. Enter the WordPress database details and then click Submit

4. Click Run the installation

5. Enter the WordPress website information and then click Install WordPress

6. Click Log In to access the WordPress admin backend to manage WordPress installation. Alternatively, you can access the WordPress backend by going to https://your-wordpress-website/wp-admin

Access WordPress Website

Now, you will be able to access the site with your domain name.

https://your-wordpress-website
WordPress Website on Apache
WordPress Website on Apache

Conclusion

That’s All. I hope you have learned how to install WordPress with Apache on Ubuntu 22.04.

You might also like