Wordpress on Apache Server Ubuntu

Page content

Install Wordpress Apache Server Ubuntu 22.04

Wordpress is an open source content management system, which is very popular for the high level of flexibility it offers. The CMS is a favorite of millions since you can build just any type of website using Wordpress, It also offers thousands of free themes and plugins to extend website functionality. From portfolio sites to blogs and ecommerce websites, you can build just anything using Wordpress. Wordpress is easy to install and can be installed on Apache or Nginx Server.

In this article, we are going to show you how to install Wordpress on an Apache server. You will need to create an instance (virtual machine) before you can install apache server. We will show you how to create an AWS EC2 instance and install Apache server and Wordpress on it. As a part of this installation, we will install:

  • Apache server

  • Wordpress

  • MySQL

  • PHP

Wordpress runs on a mysql database. You can also install a mariadb database in its place. However, for the sake of simplicity, we will use a Mysql database in this tutorial.

Create an AWS EC2 Instance

First let’s create an Amazon EC2 Instance.

  • Login to your AWS account and go to the EC2 dashboard.

  • Click on launch instances.

  • Give your instance an easy to remember name like ‘mywebserver’.

  • Select Ubuntu from the list of AMIs (Amazon Machine Image) and then Ubuntu version 22.04.

  • Create a new Key pair.

  • Create a new security group and allow SSH, HTTP and HTTPs traffic.

  • You can configure the storage according to your need.

Here, we are using a t2.micro instance. You can select a larger one based on your need. At the end, click on create instance and wait for the instance to start running.

Once the instance is running, you can SSH to it. Click on the instance from your list of instances and then click on connect. Once the connection has been established, we will need to run the updates.

sudo apt update
sudo apt upgrade

Install Apache Server

After running the updates, we can install the apache server. To install Apache server on ubuntu 22.04, run the following command:

sudo apt install apache2 -y

Once the apache server installation is complete, you can verify its status using:

sudo systemctl status apache2

You will receive an output like the following showing that the Apache server is active and running on the server.

$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2024-06-02 01:47:19 UTC; 1min 10s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 14837 (apache2)
      Tasks: 55 (limit: 1121)
     Memory: 4.9M
        CPU: 37ms
     CGroup: /system.slice/apache2.service
             ├─14837 /usr/sbin/apache2 -k start
             ├─14839 /usr/sbin/apache2 -k start
             └─14840 /usr/sbin/apache2 -k start

Jun 02 01:47:19 ip-172-31-29-225 systemd[1]: Starting The Apache HTTP Server...
Jun 02 01:47:19 ip-172-31-29-225 systemd[1]: Started The Apache HTTP Server.

Now, we can move further to install mysql server and php before we can install and run wordpress.

Create a mysql database and user

We need a mysql database and a user to run Wordpress on our Apache server. To do that, we will first need to install the mysql server. To install mysql server, run the following command:

sudo apt install mysql-server

Now, we can create a database and a user to connect Wordpress with the database.

Let’s first login to our Mysql server:

sudo mysql -u root -p

Hit enter when asked for the password since we have not set a password yet.

Now, we will create a database to use with our Wordpress installation:

CREATE DATABASE Wordpressdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;

We have created a database called Wordpressdb and now we need to create a user for our database.

CREATE USER 'wpdbuser'@'%' IDENTIFIED WITH mysql_native_password BY 'abcd@1234';

Replace wpdbuser with the username of your choice and the password ‘abcd@1234’ with a strong password. You will receive an output like the following:

Query OK, 0 rows affected (0.02 sec)

Now, let us grant the user we just created complete access to the database we have setup.

GRANT ALL ON Wordpressdb.* TO 'wpdbuser'@'%';

Now, we have the user credentials ready for Wordpress so that it can interact with the mysql database. However, we will need to flush privileges so that the changes we made can apply.

FLUSH PRIVILEGES;

Now, you can exit the mysql server:

EXIT;

Our mysql server setup is complete and we can move on to installing php before we finally install Wordpress on our server.

Install PHP on Apache server Ubuntu 22.04

Now, we will install php on our server. To install php run the following command:

sudo apt install php -y

You can check the php version with the following command:

php -v

We also need to install some additional php extensions which will be needed by various plugins. The php-mysql extension is required to help Wordpress connect with the mysql database.

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

To load the new extensions we have just installed, we will need to restart the Apache server, which can be done with the following command:

sudo systemctl restart apache2

We will need to make some additional configuration changes to our Apache server to bring our website online. First let’s create a vhosts file for our new site:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mydomain.conf

We have copied the default vhosts file and now we can make changes inside that file for our new site. We will change the document root and add ServerName and ServerAlias. Apart from that, we will also enable the use of .htaccess file on the server. You need to add the following lines to the vhosts file to enable .htaccess on your server.

    <Directory /var/www/html/wordpress>
          AllowOverride All
     </Directory>

After having made these changes, the entire vhosts file will look like the following:

<VirtualHost *:80>
        
        ServerAdmin webmaster@localhost
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/html/wordpress

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <Directory /var/www/html/wordpress>
          AllowOverride All
     </Directory>

</VirtualHost>

We are going to install Wordpress inside the /var/www/html folder. It is why our root folder is /var/www/html/wordpress because the files will be downloaded inside the folder named wordpress.

Note: We also need to make sure that we have the rewrite module active on our server so that the redirects inside the .htaccess file will work. To enable the rewrite module, run - “sudo a2enmod rewrite” and restart the Apache server. To bring the site online, we need to run the following command:

sudo a2ensite mydomain.conf

Following that reload the apache server

sudo sytemctl reload apache2

Install Wordpress

We will first need to change the permissions of /var/www/html directory so we can download files here.

sudo chown -R $USER:$USER /var/www/html

And

sudo chmod 755 /var/www/html

Now, change directory to /var/www/html.

cd /var/www/html

And download the wordpress files.

curl -O https://wordpress.org/latest.tar.gz

Now, extract those files. (These files will be extracted to the wordpress folder. Our root folder is /var/www/html/wordpress)

tar xzvf latest.tar.gz

Now, comes the time for the final installation. You should make the DNS changes so that you can access your domain by its name instead of ip. Add the A records in your DNS account.

Wait for a few minutes after having made the DNS changes and then try loading the domain in a browser. You will find yourself on the Wordpress Welcome page.

Wordpress installation

Click on let’s go and enter the database credentials in their respective boxes to install Wordpress.

install wordpress

Wordpress will create a wp-config.php file based on the credentials you provided and then you will need to create an admin user and password. After that, you can start making changes to your Wordpress site. At this point, you can create a .htaccess file and add the code to disable directory listing.

sudo nano /var/www/html/wordpress/.hatccess

Add the following to this file:

Options -Indexes

Now, you can also select the permalink structure for your website by going to settings -> permalinks. Following that you can select and enable a theme of your choice and start installing the plugins you need like a SEO plugin and a security plugin.

Suggested Reading