How to deploy WordPress on Ubuntu 22.04 using LAMP stack?

WordPress is one of the world’s most popular content management systems (CMS). It is a free and open-source platform that is used by millions of people for running blogs, e-commerce websites, and many more. WordPress contains a plugin architecture and themes, so you can customize any website.

In this blog, we will show you How to deploy WordPress on Ubuntu 22.04 using LAMP stack:

A “lamp stack” is a collection of four components of software used to create websites and web applications. The LAMP stack is a popular open-source stack used in web development. The first letters of the components names make up the LAMP acronym:

  • Linux: Linux is an open-source operating system that you can install and configure to meet different application requirements. 
  • Apache HTTP Server: Apache is an open-source web server that forms the second layer of the LAMP stack. The web server’s role is to handle requests and send data across the internet using HTTP.
  • MySQL: MySQL is an open-source relational database management system and is the third layer of the LAMP stack. MariaDB is another relational database management system that can be part of the LAMP platform.
  • PHP, Perl, and Python are programming languages are used to create web applications.

Requirement

  • For this tutorial, we are using an Ubuntu 22.04 VPS. Since we are using a clean server, we will go through all the necessary steps to install and configure the LAMP stack. Some VPS hosting plans come with a fully functional LAMP server.
  • Full SSH root access or a user with Sudo privileges is also required. 
  • A domain name if you are installing WordPress on the domain.

Connect to your Server

To connect to your server via SSH as a root user, use the following command and replace “IP_ADDRESS” AND “PORT_NUMBER” with your actual server IP address and SSH port number. Here I am using PuTTY as SSH, which has port number 22.

ssh root@IP_ADDRESS -p PORT_NUMBER

Update your System

Once you’ve logged in to SSH, make sure you update your server by running the following commands:

 sudo apt-get update
 sudo apt-get upgrade
Command to update the server

Install LAMP

STEP 1: Install the Apache Web Server

The first step is to install the Apache web server. For that, run the following command:

sudo apt-get install apache2
deploy WordPress on Ubuntu 22.04 using LAMP stack?

When the installation is complete, enable the Apache service to start automatically . To do so, execute the following command:

sudo systemctl enable apache2

To start the Apache service execute the following command:

sudo systemctl start apache2

To verify whether Apache is running or not, execute the following command:

sudo systemctl status apache2

As you can see, Apache is running and is in an active state.

Command to check the status of the apache2

To check if Apache is successfully installed, You can open your web browser and enter your server’s IP address (http://your_server_ip_address). If Apache is successfully installed, you should see the Apache default welcome page.

The default Ubuntu document root is /var/www/html.

Apache2 Default page

To know the the path in which the apache2 is installed, Execute command which apache2 it will show the path of the apache2.

STEP 2:Install the MySQL Database server

The next step is to install the Mysql database server, which will be used for the data storage of the WordPress site. To install the Mysql database server execute the following command:

sudo apt install mysql-server
Command to install mysql-server

If you want to install MariaDB, you can do so using the following command:

sudo apt install mariadb-server mariadb-client

To improve the security of our MySQL installation and to set the password for the MySQL root user, we need to run the mysql_secure_installation script. For that, execute the following command:

sudo mysql_secure_installation
Command to install the secure Mysql.

If the program asks you to enter your current MySQL root password, just press your [Enter] key once, as no password is set by default when installing MySQL.

After you have answered all the questions, you can start the database server and enable it to run automatically with the following command:

sudo systemctl start mysql
sudo systemctl enable mysql

To verify whether mysql is running or not, execute the following command:

sudo systemctl status mysql

And it will prompt you for a password; enter the password you used when installing sudo mysql_secure_installation. Remember that after you enter the MySQL password, you must open MySQL using these command .

sudo mysql -u root -p
Command to check the  status of the mysql

To check the version your MySQL is running, type and execute mysql -V(note the uppercase V) in the command line.

mysql -V

STEP 3: Install PHP 

The third step of the LAMP stack is to install PHP. Since WordPress is a PHP-based CMS, we need PHP for the dynamic content of our WordPress site. Execute the following command to install PHP:

sudo apt install php
Command to install PHP

Since WordPress and many of its plugins use PHP extensions, you need to install some of the PHP extensions. Execute the following command, and it will install the packages.

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

To check the version of your php, execute this command. we have installed 8.1.2version of PHP.

php -v

To check whether PHP is working or not, we will create a sample PHP file in the web root directory which is /var/www/html. Use this command to go to the directory.

cd /var/www/html

Then use this command to create a sample PHP file and open it using the text editor. I have used vi text editor you can use according to your convivence.

vi sample.php

In the text editor, insert the following code:

<?php
phpinfo();
?>

Save and exit the file.

Now, access the file by entering http://your-IP-address/sample.php in your web browser. You should see this PHP info page:

STEP 4: Install WordPress

Now we are ready to install WordPress because our LAMP environment is fully installed and configured. The WordPress installation is done in the /var/www/html directory, which is the default document root for the web servers. You can move to this directory using the following command:

cd /var/www/html

You can now download the latest WordPress installation with the following command:

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

After that, we have to extract the WordPress files:

tar -xzvf latest.tar.gz

The extracted WordPress files will be now placed in the WordPress directory at the following location on your server /var/www/html/WordPress.

The owner of these files needs to be the user of the web server running on your system. In our example, we are using the Apache web server, and Apache runs as the www-data user on Ubuntu 22.04. To change the owner and set the correct permissions for these files, you need to run the following command:

sudo chown -R www-data:www-data /var/www/html/wordpress

Creating a Database for WordPress

Next, we will create our MySQL user and database for our WordPress site. Log in to your MySQL server with the following command and enter your MySQL root password:

mysql -u root -p
Command to login to mysql server as a root

To create a new database for our WordPress installation, run the following commands.I have used wp_wordpress as a DATABASE name and risenepal as a USER.

mysql> CREATE DATABASE (Your database name);
mysql> CREATE USER 'username '@localhost IDENTIFIED BY 'password'; 
mysql> GRANT ALL PRIVILEGES ON (Your database name).* TO (user)@localhost;
mysql>FLUSH PRIVILEGES;
mysql>EXIT;

If you want to check whether your DATABASE is created or not ,run the following command. As you can wp_wordpress database is created.

mysql>SHOW DATABASES;

If you want to see the user in your mysql database

SELECT user FROM mysql.user;

You can replace the database name (wp_wordpress) and the MySQL user name (risenepal) with your own names if you wish. Also, make sure to use a “strong-password” .

Once the database is created, we will need to add this information to the WordPress configuration file.

Make sure you are inside the /var/www/html/WordPress directory:

cd /var/www/html/wordpress

Then run the following command to rename the sample configuration file:

mv wp-config-sample.php wp-config.php

Now open the wp-config.php file with your favorite text editor, for example I have used vi text editor:

vi wp-config.php

And update the database settings, replacing wp_wordpressrisenepal and strong_password with your own details:

  • The name of the database for WordPress */define(‘DB_NAME’, ‘wp_wordpress’);
  • MySQL database username */define(‘DB_USERNAME’, ‘risenepal’);
  • MySQL database password*/define(‘DB_PASSWORD’, ‘strong-password’);
  • MySQL hostname */define(‘DB_HOST’, ‘localhost’);
  • Database Charset to use in creating database tables.*/define(‘DB_CHARSET’, ‘utf8’);
  • The Database Collate type. Don’t change this if in doubt. */define(‘DB_COLLATE’, ‘ ‘);

Save and exit the file.

Restart your Apache and MySQL server with:

systemctl restart apache2
systemctl restart mysql

Save and exit the file.

With this being done, you can now access your WordPress page and finish the installation by following the on-screen instructions in your browser at http://your_server_ip_address/wordpress.

Choose your language and click “Continue”.

Enter your preferred information at the main installation screen, such as site title, username, password and email, and click on “Install WordPress”.

After a successful login, you will be greeted by the WordPress dashboard page.

Configure an Apache Virtual Host

If you have a valid domain name and want to use it to access your WordPress instance, you will need to create a new Apache virtual host file. Let’s say we want to access our WordPress with the risenepal.com domain name. Wherever you see a risenepal.com show up in the blog make sure to replace it with your domain name to make sure that everything is named correctly.

First, run the following command to create the virtual host configuration file :

vi /etc/apache2/sites-available/risenepal.com.conf

And add the following content to the file (you can replace risenepal.com with your actual domain name):

<VirtualHost *:80>

ServerAdmin admin@risenepal.com
ServerName risenepal.com
ServerAlias www.risenepal.com
DocumentRoot /var/www/html/wordpress


ErrorLog ${APACHE_LOG_DIR}/risenepal.com_error.log
CustomLog ${APACHE_LOG_DIR}/risenepal.com_access.log combined
</VirtualHost>

To enable the virtual host we have just created, run the following command:

a2ensite mydomain.com.conf

Once you enable the virtual host, using either of the above methods, you will need to restart the Apache web server:

systemctl restart apache2

If the DNS records are properly configured, and your domain is pointed to your server, you can use it to access your WordPress installation by typing http://mydomain.com in your browser.

Installing SSL to the domain

You have installed WordPress on your VPS, and now we have to secure it. The objective of Let’s Encrypt is to make it possible to set up an HTTPS server and have it automatically obtain a browser-trusted certificate, without any human intervention. This is accomplished by running a certificate management agent on the web server.

Let’s Encrypt is a service offering free SSL certificates through an automated API. The most popular Let’s Encrypt client is EEF’s Certbot.

Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache.

STEP 1: Installing Certbot Package

To obtain an SSL certificate with Let’s Encrypt, you need to install the Certbot software on your server. First, update the local package index:

sudo apt update

You need two packages: certbot, and python3-certbot-apache. Start the installation process of Certbot and python3-certbot-apache, using the command given below:

sudo apt install certbot python3-certbot-apache

You will be prompted to confirm the installation by pressing Y, then ENTER.

Certbot is now installed on your server. In the next step, you’ll verify Apache’s configuration to make sure your virtual host is set appropriately. This will ensure that the certbot client script will be able to detect your domains and reconfigure your web server to use your newly generated SSL certificate automatically.

Step 2: Enable and Configure the firewall

To enable the firewall to allow HTTPS traffic for the configuration of the SSL certificate, we will use Ubuntu’s by default and pre-installed UFW to modify the firewall rules.

To know about the UFW rules currently running on the system, type the below-given command

sudo ufw status

If the status is inactive, enable the ufw utility using the command,But before that we have to enable the ssh.

sudo ufw allow ssh
sudo ufw allow 'Apache Full'
sudo ufw allow http
sudo ufw allow https

After that we will enable ufw by running this command:

sudo ufw enable

Check the status of UFW again to verify the configuration using the command mentioned below:

sudo ufw status

STEP 3: Get the Let’s Encrypt SSL certificate

To get the SSL certificate using the Certbot, type the command given below:

sudo certbot --apache -d risenepal.com -d www.risenepal.com

The execution of the above command will prompt a couple of questions, so read them carefully and answer accordingly as per your requirements and needs.

First, provide the email address:

Next, Agree to the Terms of Service by typing and pressing Enter:

Later, it will ask for sharing your email address with the EFF(Electronic Frontier Foundation), so type Y if you want to share or N if you do not want to share your email address:

After answering all the questions, the installation will start, and you will have the new SSL certificate. As you can see my domain is secure now.

STEP 4: Verify the Certbot service status

Afterward, to verify the Certbot auto-renewal, just check the status of the service using the command:

 sudo systemctl status certbot.timer

Conclusion

Congratulation ! WordPress has been successfully installed on your server.

Leave a Reply

Your email address will not be published. Required fields are marked *