Setting Up a LEMP Stack with WordPress on Microsoft Azure

Introduction

The LEMP stack (Linux, Nginx, MySQL, PHP) is a popular open-source software bundle used to host dynamic websites and applications. This stack leverages the performance and flexibility of Linux as the operating system, Nginx as the high-performance web server, MySQL as the robust database management system, and PHP as the server-side scripting language. Hosting WordPress on a LEMP stack ensures a fast, scalable, and secure environment capable of handling high traffic and complex web applications.

Deploying a LEMP stack on the Azure cloud platform offers numerous advantages, including scalability, reliability, and security. Azure provides a global network of data centers, enabling low-latency access and high availability for your WordPress site. Moreover, Azure’s integrated services, such as monitoring, backup, and security features, simplify the management and maintenance of your infrastructure. By leveraging Azure’s pay-as-you-go model, you can optimize costs and scale resources according to your website’s demands.

Prerequisites

Before starting, ensure you have the following Azure services and resources:

  • Azure Subscription: An active Azure subscription is required to create and manage resources.
  • Resource Group: A logical container to hold related resources.
  • Virtual Machine (VM): An Ubuntu Linux VM to host the LEMP stack.
  • MySQL Flexible Server: A managed database service for the MySQL database.
  • DNS Label: A unique DNS label for accessing the VM.

Make sure you have the necessary user accounts and permissions:

  • Azure Account: Ensure you have an Azure account with sufficient permissions to create and manage resources.
  • SSH Key Pair: Required for secure access to the VM.
  • Admin Credentials: Administrative credentials for managing MySQL and WordPress.

Step-by-Step Instructions

1. Create and Configure Azure Resources

1.1 Create a Resource Group

  1. Sign in to the Azure portal.
  2. Navigate to Resource groups and select Create.
  3. Enter a name for the resource group and select a region.
  4. Click Review + create and then Create.

1.2 Create an Ubuntu Linux VM

  1. In the Azure portal, navigate to Virtual machines and select Create.
  2. Choose Azure Virtual Machine and complete the Basics tab:
    • Select the resource group created earlier.
    • Enter a VM name (e.g., myVM).
    • Choose the region.
    • Select an appropriate VM size (e.g., Standard_DS2_v2).
    • Configure the authentication type with your SSH public key.
  3. In the Networking tab, configure the network settings:
    • Create a new virtual network and subnet.
    • Assign a public IP address.
    • Open ports 80 (HTTP) and 443 (HTTPS) for web traffic.
  4. Click Review + create and then Create.

1.3 Create a MySQL Flexible Server

  1. Navigate to Azure Database for MySQL and select Create.
  2. Complete the Basics tab:
    • Select the resource group.
    • Enter a server name (e.g., myMySQLServer).
    • Choose the region.
    • Set the admin username and password.
  3. In the Networking tab, configure the network settings to allow access from your VM.
  4. Click Review + create and then Create.

2. Install and Configure the LEMP Stack

2.1 Access the VM

Use SSH to connect to the VM:
ssh azureadmin@<your-public-ip>

2.2 Install Nginx

Update the package list and install Nginx:
sudo apt update sudo apt install nginx

Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx

2.3 Install MySQL

Install MySQL server:
sudo apt install mysql-server

Secure the MySQL installation:
sudo mysql_secure_installation

Create a WordPress database and user:
CREATE DATABASE wordpress;
CREATE USER ‘wpuser’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON wordpress.* TO ‘wpuser’@’localhost’;
FLUSH PRIVILEGES;

2.4 Install PHP

Install PHP and required extensions
sudo apt install php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-xml php-xmlrpc php-zip

Configure PHP for Nginx
sudo nano /etc/nginx/sites-available/default

Add the following lines within the server block:nginx 

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
Restart Nginx:
sudo systemctl restart nginx

3. Deploy and Configure WordPress

3.1 Download WordPress

Download and extract WordPress

cd /var/www
sudo curl -O https://wordpress.org/latest.tar.gz
sudo tar xzvf latest.tar.gz
sudo mv wordpress/* /var/www/html/

Set the correct permissions
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

3.2 Configure WordPress

Create the WordPress configuration file
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo nano /var/www/html/wp-config.php


Update the database settings:php 


define(‘DB_NAME’, ‘wordpress’);
define(‘DB_USER’, ‘wpuser’);
define(‘DB_PASSWORD’, ‘password’);
define(‘DB_HOST’, ‘localhost’);

Finalize the installation by navigating to http://<your-public-ip> in your web browser and following the on-screen instructions.

Troubleshooting and Best Practices

Common Issues

  • Permission Errors: Ensure the correct file and directory permissions are set for the WordPress installation.
  • Database Connection Issues: Verify the database credentials in the wp-config.php file and ensure MySQL is running.
  • Nginx Configuration Errors: Check the Nginx configuration file for syntax errors using sudo nginx -t.

Best Practices

  • Regular Backups: Use Azure Backup to schedule regular backups of your VM and MySQL database.
  • Security: Implement SSL/TLS using Let’s Encrypt to secure your WordPress site.
  • Updates: Regularly update the system packages, Nginx, MySQL, and PHP to the latest versions.
  • Monitoring: Use Azure Monitor to keep track of the performance and health of your VM and database.

By following these instructions, you can successfully set up a LEMP stack with WordPress on the Microsoft Azure cloud platform, providing a robust and scalable environment for your website or application.

AzureLEMP StackLinuxMicrosoftwordpress
Comments (0)
Add Comment