A Step-by-Step Guide to Installing WordPress with MySQL, Nginx, and Let’s Encrypt on Ubuntu

If you’re looking to set up a WordPress website using Ubuntu, Nginx, MySQL, and secure it with Let’s Encrypt, you’ve come to the right place. This blog post will guide you through the entire process step-by-step, ensuring you get your WordPress site up and running smoothly.

Prerequisites

Before we begin, make sure you have the following:

  1. A server running Ubuntu (20.04 or above).
  2. A root or sudo user access.
  3. A domain name pointing to your server’s public IP address.

Step 1: Update Your Server

Open your terminal and connect to your server. The first step in any installation is to ensure your packages are up to date. Run:

sudo apt update
sudo apt upgrade -y

Step 2: Install Nginx

Nginx will act as your web server. To install Nginx, run:

sudo apt install nginx -y

Once installed, start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify that Nginx is working, you can go to your server’s public IP address or domain name in your web browser. You should see the Nginx default welcome page.

Step 3: Install MySQL

Next, we’ll need to install MySQL to handle the database for WordPress. Install MySQL server by running:

sudo apt install mysql-server -y

After installation, secure your MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and configure other security settings.

Step 4: Create a MySQL Database and User for WordPress

Log into MySQL:

sudo mysql -u root -p

Now, create a database and user for your WordPress installation:

CREATE DATABASE wordpress_db;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Install PHP and Extensions

WordPress is built on PHP, so we’ll need to install PHP and related extensions. Use the following commands:

sudo apt install php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd -y

Next, configure PHP Processor. Open /etc/php/7.x/fpm/php.ini in your favorite text editor (replace 7.x with your installed PHP version):

sudo nano /etc/php/7.x/fpm/php.ini

Find and adjust the following settings:

cgi.fix_pathinfo=0

Save the file and exit.

Restart the PHP processor:

sudo systemctl restart php7.x-fpm

Step 6: Configure Nginx for WordPress

We need to create a new configuration file for our WordPress site. Create a new file in /etc/nginx/sites-available/ called wordpress:

sudo nano /etc/nginx/sites-available/wordpress

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;

    root /var/www/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.x-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Remember to replace your_domain.com with your actual domain name and 7.x with your installed PHP version.

Step 7: Enable the Nginx Configuration

Link this configuration file to the sites-enabled directory and remove the default configuration:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Test your Nginx configuration for syntax errors:

sudo nginx -t

If everything is good, restart Nginx:

sudo systemctl restart nginx

Step 8: Install WordPress

Create a directory for your WordPress files:

sudo mkdir /var/www/wordpress

Download the latest version of WordPress:

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

Move the WordPress files to the directory we created earlier:

sudo mv wordpress/* /var/www/wordpress/

Change ownership and permissions:

sudo chown -R www-data:www-data /var/www/wordpress
sudo find /var/www/wordpress -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress -type f -exec chmod 640 {} \;

Step 9: Complete Your Installation via the Web Interface

Visit http://your_domain.com in your web browser. You will see the WordPress installation wizard. Follow the wizard to complete the setup by entering your database details created earlier.

Step 10: Install Let’s Encrypt for SSL

To secure your website with HTTPS, we’ll use Let’s Encrypt. First, install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Run Certbot to obtain and install your SSL certificate:

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Follow the prompts, and Certbot will automatically configure Nginx for SSL.

Step 11: Set Up Auto-Renewal for Let’s Encrypt

Let’s Encrypt certificates are valid for 90 days. You can set up a cron job to renew your certificates automatically. Test automatic renewal by running:

sudo certbot renew --dry-run

If successful, you can set the following cron job:

sudo crontab -e

Add the following line:

0 0 * * * /usr/bin/certbot renew >> /var/log/certbot-renew.log

Conclusion

Congratulations! You’ve successfully installed WordPress with MySQL, Nginx, and secured it with Let’s Encrypt on an Ubuntu server. You can now start customizing your WordPress website, choosing themes, plugins, and, most importantly, creating content for your audience!

If you have any questions or run into issues during installation, feel free to drop a comment below. Happy blogging!

Leave a Comment