Wordpress-Nginx

How To Install WordPress with Nginx on Ubuntu 24.04

Welcome to the Greenhost.Cloud blog! In today’s post, we’ll walk you through the process of installing WordPress using Nginx on Ubuntu 24.04. WordPress is one of the most popular content management systems (CMS), and Nginx is a high-performance web server that can help your site run efficiently. Whether you’re setting up a new blog, portfolio, or business website, this guide will provide a step-by-step approach to getting your WordPress site live.

Prerequisites

Before we begin, ensure you have the following:

  1. Ubuntu 24.04 Server: You should have a fresh installation of Ubuntu 24.04 running.
  2. Root Access or Sudo Privileges: You’ll need to have root access or be able to use sudo for executing commands.
  3. Basic Knowledge: Familiarity with the terminal and command-line interface is beneficial.

Step 1: Update Your System

It’s always a good practice to start by updating your system. Run the following command to ensure all packages are up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Next, we’ll install Nginx. You can do this with the following command:

sudo apt install nginx -y

Once installed, you can check if Nginx is running by accessing your server’s public IP address in your web browser. You should see the Nginx welcome page. You can also check the status using:

sudo systemctl status nginx

Step 3: Install MySQL

WordPress requires a database, and we’ll use MySQL for this. Install MySQL with the following command:

sudo apt install mysql-server -y

After installation, secure your MySQL installation by running:

sudo mysql_secure_installation

Follow the prompts to set a root password and secure your database installation.

Step 4: Create a MySQL Database and User

Next, we’ll create a MySQL database and user for WordPress. Log in to the MySQL shell:

sudo mysql -u root -p

Once logged in, create a database and user, and grant privileges:

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

Make sure to replace your_password with a strong password of your choice.

Step 5: Install PHP

WordPress is built with PHP, so we need to install it along with some necessary extensions. Install PHP and the required extensions with the following command:

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

Once installed, ensure that the PHP service starts automatically on boot:

sudo systemctl enable php-fpm

Step 6: Download and Configure WordPress

Now, let’s download the latest version of WordPress and configure it.

Download WordPress

Navigate to your web directory (usually /var/www/html) and download WordPress:

cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz

Configure WordPress

Copy the sample configuration file and edit it:

sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php

In this file, find the lines that define the database name, user, and password. Modify them to match the database you created earlier:

define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'your_password');

Step 7: Configure Nginx

We need to create a new Nginx server block for our WordPress site. Create a new configuration file:

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

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com;  # Replace with your domain or IP
    root /var/www/html;

    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/php8.1-fpm.sock;  # Adjust for your PHP version
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Enable the Configuration

Create a symbolic link to enable the site and test the Nginx configuration:

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t

Restart Nginx

To apply the changes, restart Nginx:

sudo systemctl restart nginx

Step 8: Finish the WordPress Installation

Visit your web browser and navigate to http://your_domain.com (replace with your domain or IP address). You should see the WordPress installation wizard. Follow the prompts to complete the installation, including setting up your site title, username, password, and email.

Conclusion

Congratulations! You have successfully installed WordPress with Nginx on Ubuntu 24.04. You can now start customizing your site with themes, plugins, and content. If you have any questions or need further assistance, feel free to reach out in the comments below. Happy blogging!


Don’t forget to subscribe to the Greenhost.Cloud blog for more tutorials on web hosting, server management, and development!