CMS

How to Install WordPress, Nginx, PHP, and Varnish on Ubuntu 24.04

Welcome to the Greenhost.Cloud blog! In this guide, we’ll walk you through the installation of WordPress, Nginx, PHP, and Varnish on Ubuntu 24.04. This powerful combination allows you to host a high-performance WordPress website that can efficiently handle high traffic while ensuring a seamless experience for your users.

Prerequisites

Before we begin, ensure you have:

  1. A server running Ubuntu 24.04.
  2. Root or sudo access to the server.
  3. Basic knowledge of using the terminal.

Let’s get started!

Step 1: Update Your System

First, log in to your Ubuntu server and run the following commands to update your package list and upgrade any installed packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Nginx is a high-performance web server that will serve as our primary server for hosting WordPress.

sudo apt install nginx -y

After installation, you can use the following command to start Nginx and ensure it runs on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

To check the status of Nginx, use:

sudo systemctl status nginx

Visit your server’s IP address in a web browser, and you should see the Nginx default welcome page.

Step 3: Install PHP and Required Extensions

WordPress is built on PHP, so we need to install PHP along with necessary extensions.

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

After the installation, pause and configure PHP-FPM. Open the php.ini file:

sudo nano /etc/php/8.1/fpm/php.ini

Search for the following lines and adjust the values as necessary:

cgi.fix_pathinfo=0

Save the file and exit, then restart PHP-FPM to apply the changes:

sudo systemctl restart php8.1-fpm

Step 4: Install MySQL

WordPress requires a database, so we’ll install MySQL.

sudo apt install mysql-server -y

Secure your MySQL installation:

sudo mysql_secure_installation

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

Next, log into MySQL to create a database and a user for WordPress:

sudo mysql -u root -p

Once logged in, run the following commands to create a database and user:

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

Step 5: Install WordPress

Now we will download and configure WordPress.

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz

Next, move the WordPress files to the web root:

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

Step 6: Configure Nginx

Create an Nginx server block for your WordPress site:

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

Add the following configuration (be sure to replace example.com with your domain):

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

    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:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Save and exit, then enable the server block and test the configuration:

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

Finally, restart Nginx:

sudo systemctl restart nginx

Step 7: Install Varnish

Varnish is a caching HTTP reverse proxy that will sit in front of Nginx to improve performance.

sudo apt install varnish -y

Edit the Varnish default configuration file:

sudo nano /etc/varnish/default.vcl

Update the backend section with:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Next, configure Varnish to listen on port 80 and Nginx to listen on port 8080. Open the Varnish systemd service file:

sudo nano /etc/systemd/system/varnish.service

Find the ExecStart directive and change it to:

ExecStart=/usr/bin/varnishd -a :80 -T localhost:6081 -f /etc/varnish/default.vcl -p default_ttl=120 -p default_grace=20

Now change Nginx to use port 8080. Open the nginx.conf file:

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

Update the first line in the server block:

listen 8080;

Restart Varnish and Nginx to apply the changes:

sudo systemctl restart varnish
sudo systemctl restart nginx

Step 8: Complete WordPress Installation

Finally, navigate to http://your_domain_or_IP in your browser and complete the WordPress installation by following the setup prompts.

Conclusion

You have now successfully installed WordPress on Ubuntu 24.04 with Nginx, PHP, and Varnish! Your site will benefit from the speedy performance provided by Nginx and Varnish, ensuring a great user experience.

Feel free to explore further optimizations and settings based on your specific needs and traffic levels. As always, keep your software up to date and back up your data regularly.

For any questions or comments, please leave them below!

Happy blogging!