In this tutorial, we will learn how to install WordPress on Ubuntu 22.04 using LAMP. We will also show you how to secure WordPress with Let’s Encrypt SSL.
First, it is always a good idea to update and upgrade all system packages to the latest version. You run following commands to update all packages.
apt update -y
apt upgrade -y
After updating all system packages, you can proceed to install the LAMP server.
LAMP stands for Linux, Apache, MariaDB and PHP. You can install all these components using the following command.
apt install apache2 mariadb-server php php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap libapache2-mod-php php-mysql -y
After installing the LAMP server, start and enable the Apache service using the following command.
systemctl start apache2
systemctl enable apache2
At this point, the LAMP server is installed on your server. You can now proceed to create a database for WordPress.
WordPress uses a MariaDB as a database backend to store their content. First, secure the MariaDB installation and set a root password using the following command.
mysql_secure_installation
Answer all the questions as shown below to secure the MariaDB installation.
Enter current password for root (enter for none): Press ENTER
Set root password? [Y/n]: Y
New password: Set-your-new-password
Re-enter new password: Set-your-new-password
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Next, log in to the MariaDB shell using the following command:
mysql -u root -p
After log in, create a wordpress database and user with the following command.
CREATE DATABASE wordpress;
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'yourpassword';
Next, grant all the privileges to the wordpress database with the following command.
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';
Then, flush the privileges and exit from the MariaDB shell with the following command.
FLUSH PRIVILEGES;
EXIT;
Now, change your directory to the Apache website directory and download the latest version of WordPress with the following command.
cd /var/www/html
wget http://wordpress.org/latest.tar.gz
Once the WordPress is downloaded, extract the downloaded file with the following command.
tar -xvzf latest.tar.gz
Next, change the directory to the WordPress directory and rename the default configuration file.
cd wordpress
mv wp-config-sample.php wp-config.php
Then, edit the WordPress configuration file and define your database settings.
nano wp-config.php
Change the following lines:
define( 'DB_NAME', 'wordpress' );
/** Database username */
define( 'DB_USER', 'wordpress' );
/** Database password */
define( 'DB_PASSWORD', 'yourpassword' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
Save and close the file then change the ownership and permission of the WordPress directory.
chown -R www-data:www-data /var/www/html/wordpress
chmod -R 775 /var/www/html/wordpress
Next, you need to create an Apache virtual host configuration file for WordPress. You can create it with the following command.
nano /etc/apache2/sites-available/wordpress.conf
Add the following lines:
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
DocumentRoot /var/www/html/wordpress
ServerName wp.yourdomain.com
<Directory /var/www/html/wordpress/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
Save the file then activate the Apache virtual host and rewrite module using the following command.
a2ensite wordpress.conf
a2enmod rewrite
Next, restart the Apache service to apply the configuration changes.
systemctl restart apache2
To check the Apache service status, run the following command.
systemctl status apache2
You will get the following output.
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-12-24 14:35:38 UTC; 2s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 53484 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 53488 (apache2)
Tasks: 6 (limit: 1030)
Memory: 15.1M
CPU: 37ms
CGroup: /system.slice/apache2.service
├─53488 /usr/sbin/apache2 -k start
├─53489 /usr/sbin/apache2 -k start
├─53490 /usr/sbin/apache2 -k start
├─53491 /usr/sbin/apache2 -k start
├─53492 /usr/sbin/apache2 -k start
└─53493 /usr/sbin/apache2 -k start
Dec 24 14:35:38 ubuntu2204 systemd[1]: apache2.service: Deactivated successfully.
Dec 24 14:35:38 ubuntu2204 systemd[1]: Stopped The Apache HTTP Server.
Dec 24 14:35:38 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...
To install the Let’s Encrypt SSL, you will need to install the Certbot client on your server. You install it with the following command.
apt 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 wp.yourdomain.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): hitjethva1981@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 wp.yourdomain.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://wp.yourdomain.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=wp.yourdomain.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/wp.yourdomain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/wp.yourdomain.com/privkey.pem
Your cert will expire on 2023-02-29. 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, open your web browser and perform the WordPress web installation using the URL https://wp.yourdomain.com. You should see the WordPress language selection
Thank you for reading How to Install WordPress on Ubuntu 22.04 using LAMP. We shall conclude the article.