WordPress on Azure

Install  WordPress on Azure VM Instance

Azure (Microsoft’s Cloud offering) offers a nice range of VM instances for various types of workloads that you can use to bring a website online in minutes. These instances can be great if you want to host wordpress and run your own blog using Azure VM Instance. While, it may sound intimidating at first, it is not really as complex.

If you are planning to start a wordpress blog, you can set it up using Azure VM instance of your preferred size. You can install and run wordpress on one of the smallest instance sizes. For example, you can start with 1GB RAM or an instance that will not cost more than $10 a month. This is still a lot cheaper than what most hosting providers offer for this price since you will get fantastic speed apart from lower cost.

Running wordpress will require nothing but some technical knowledge, like the ability to run commands and connect to your instance. However, if you have at least the basic technical skills, you can easily do things using the Azure CLI or Azure Command Line Interface, which is a great tool to manage your Azure account and use various Azure services. Otherwise, you can use the Azure console to create instances and start your own blog.

Create Azure VM Instance

The process of wordpress installation will require two things first – A VM Instance and a webserver.

So, in the first step, you will need to create a VM Instance. To create the instance, search for VM instances and then click on it. Click on Create and it will open a form. You need to have a resource group to use Azure services like VM instances. If you have not created a resource group, no problem. You can create a new one while creating a VM Instance. If you leave the resource group field blank, the system will create and name a new resource group corresponding to the name of the VM instance.

For resource group, select create new and give your resource group a name. Enter a name (any name) for your VM instance like myVM. Use a short name for your resource group and VM instance since it will make it easier to run Azure cli commands.

After naming your VM instance, select a machine image like Ubuntu 20.04 or Ubuntu 22.04 and then select a size. Browse all VM sizes to select your preferred one.

For SSH connection, you will need to create an adminuser. You can use public keys or password for authentication. If you select to use a password, create a strong password.

You will see a section – inbound rules – towards the bottom of the form. If you are unfamiliar with the term, it includes rules to allow inbound traffic to your instance on specified ports through the firewall. Select to allow traffic on specified ports and then select the three ports 22, 80 and 443. In case you are unfamiliar with the concept of ports, port 22 is used for SSH connection, port 80 for http traffic and port 443 for https.

The default settings in networking section are fine if you are planning to start a simple blog. Most things are easy to set including adding tags to your instance. You can also select the size of the disk on the first page of the input form for instance creation. When you are done, click review and create. The system will first validate the settings and once validation is passed, you can click on create. In a few minutes, your VM instance is deployed and ready.

SSH to your instance

Now, you can connect to your instance using SSH.

$ ssh username@publicip

Once you are connected to your instance, just install NGINX webserver. NGINX is a very fast webserver, which is also used as a reverse proxy and load balancer. If you are concerned for speed, using NGINX server is your best option. It is a better option in terms of caching, speed and security.

Install NGINX Web Server

Installing NGINX server is not a complex task and can be completed with a just a few commands. You can run updates and then install the server:

$ sudo apt-get update && sudo apt-get install -y nginx

Once installation is complete, you can check the status of your webserver with the following command:

$ sudo systemctl status nginx

Now, enable nginx and allow it through the firewall.

$ sudo systemctl enable nginx

$sudo ufw allow ‘Nginx’

At this stage, if you copy your public ip and paste it in a browser, you will be greeted by the default nginx page which shows you have successfully installed NGINX.

The default configuration for your nginx server is available inside the nginx.conf file, which you can browse using:

$ sudo nano /etc/nginx/nginx.conf

After having installed the nginx webserver, you can proceed to install wordpress. To run wordpress, you will also need mysql database and PHP latest version. After having installed these two, we can download and install the latest version of wordpress on our VM Instance.

Install mysql database

You can install mysql database with a simple one line command. Following the installation, the mysql database will be running on your server. You can check the status of the database after the installation.

azureuser@myVM:~$ sudo apt install mysql-server

The above command will install the mysql database on your server. You can check its status with the following command and the output will show that mysql is active and running.

azureuser@myVM:~$ sudo systemctl status mysql

The output will look like the following:

mysql.service – MySQL Community Server

     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)

     Active: active (running) since Fri 2023-03-31 10:44:08 UTC; 36s ago

    Process: 11728 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)

   Main PID: 11736 (mysqld)

     Status: “Server is operational”

      Tasks: 39 (limit: 1077)

     Memory: 357.1M

        CPU: 1.316s

     CGroup: /system.slice/mysql.service

             └─11736 /usr/sbin/mysqld

Now, login to mysql:

azureuser@myVM:~$ sudo mysql -u root -p

The system will ask for a password but since we have not set one yet, just hit enter.

Now, you are inside mysql. So, run the command to create a database named WordPressdb and user named dbuser for your database.

$ CREATE DATABASE WordPressdb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

$ CREATE USER 'dbuser'@'localhost' IDENTIFIED BY 'strong_password';

Now, grant all privileges on the database to the user you have created;

$ GRANT ALL PRIVILEGES ON WordPressdb.* TO 'dbuser'@'localhost';

Now, flush the privileges so that the changes can take effect, and exit mysql:

$ FLUSH PRIVILEGES;

$ EXIT;

You have installed the mysql database and also created the user and password that wordpress will use to connect with the database.  The remaining parts include php and wordpress installation.

Install PHP

You can install PHP with a single one line command that will automatically install the latest version of PHP on your server.

$ sudo apt install php-fpm

You will be able to see the PHP version in the output of the above command. Accordingly you can alter the below command to check the status of php-fpm and if it is working correctly.

$sudo systemctl status php8.1-fpm

You also need to install the php-mysql extension so wordpress can work with the mysql database. Otherwise, you will receive a data connection error or mysqli extension not installed error when installing wordpress. Install it with the following command:

$ sudo apt-get install php-mysql

Optionally, you can also install the php-gd extension.

$ sudo apt-get install php-gd

You have now installed everything that is required to run wordpress.

Install wordpress on Azure VM Instance:

Now, it is time to install wordpress on Azure VM instance. To do that, we will first need to download the latest version of wordpress. The latest version of wordpress is always available at the following link, which you can download using the wget command:

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

Run the following command to download wordpress:

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

 And the following command to extract the contents of the file:

$ tar -xzf latest.tar.gz

The above command will extract the contents of the wordpress installation file to a folder named wordpress. The idea is to download the installation files to wordpress folder and thne move them to the website root folder after configuration.

Now, it is the time to set the wp-config.php file which contains the configurations for running wordpress. The wordpress installation folder includes wp-config-sample.php which you can use to create your wp-config.php. First cop the contents of the wp-config-sample.php to wp-config.php

$ cp wordpress/wp-config-sample.php wordpress/wp-config.php

Now, open and edit the wp-config.php file.

$ sudo nano wordpress/wp-config.php

Inside this file, replace the values for DB_NAME, DB_USER and DB_Password with the ones you created after installing mysql. WordPress will use these credentials to connect with the database.

To further secure your wordpress site, add authentication unique keys and salts to the wp-config.php file. You will find this section after scrolling down a bit. To generate random keys and salts, go to the following link and paste the values from there to your wp-config.php for each item.

Now, we can move the wordpress files to the root directory from where we want to run wordpress. For example, you can run it from /var/www/html or create a new folder /var/www/html/wordpress/blog for your wordpress installation. In the second case, you will need to create the directory and then move the contents of the wordpress folder. The mkdir command is used to create a directory.

$ mkdir /var/www/html/wordpress/blog

Grant ownership of the folder to the current user:

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

Now, copy the contents of wordpress folder and move them to your website root folder.

$ cp -r wordpress/* /var/www/html/wordpress/blog/

Before the final installation of wordpress and accessing the admin dashboard, enable mysql with the following command:

$ sudo systemctl enable mysql

The output will look like the following:

"Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable mysql"

There is one thing that still remains to be done before we can bring our wordpress blog online with a custom domain name. We need to create a server configuration for the blog (like vhost files in Apache server). Create a configuration file for your website like example.com.conf inside the following folder: /etc/nginx/conf.d

Run the following command. Replace example.com with your domain name.

$ sudo nano /etc/nginx/conf.d/example.com.conf

Now, paste the following content into this file. Replace example.com with your domain name and the root folder with the path to your selected root folder.

server {

              listen 80;

              listen [::]:80;       

             root /var/www/html/wordpress/blog

            index index.html index.php index.htm index.nginx-debian.html;

           server_name example.com www.example.com;

location / {

                try_files $uri $uri/ /index.php?$args;

        }

  location ~ \.php$ {

        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/run/php/php8.1-fpm.sock;

        include fastcgi_params;

}

}

Now, close the file and exit. (Ctrl X, Y and enter)

Everything is ready and you can access your wordpress installation using the public ip of your instance. However, you need to add a custom domain before you can make your blog publicly accessible. The above server configuration is done for the same purpose.

You just need to add the necessary DNS records. For example, if you are using a premium DNS host like Azure DNS or Route 53 or Cloudflare, add A records for your domain and www subdomain pointing to the public ip of your Azure VM instance. Now, you will be able to access the wordpress installation using your domain name. Type your domain name in a browser and go.

When you load your domain name for the first time after installing wordpress, you are directed to the wordpress configuration page, where you are required to create an admin user name and password and provide a name for your blog. Add your email inside the last box and hit install wordpress.

The installation process will hardly take a minute and following that you will  be required to login once again with the admin username and password you just created.

You can now login to your wordpress admin and check out your brand-new blog. Try loading the home page and the default hello world page. You will see how fast the wordpress site loads on NGINX webserver. Now, you can start editing according to your choice. Add and replace themes and plugins and start adding content. You have successfully installed wordpress on Azure VM instance.

Thanks for reading!