How to deploy wordpress website with nginx and mysql
Tags: programming, nginx, wordpress, mysql
Published: 2023-12-17 10:12:22

Deploying a WordPress website with Nginx and MySQL involves several steps. Here’s a step-by-step guide to help you set up and deploy your WordPress site. This guide assumes you have a server or hosting environment where you can install and configure Nginx and MySQL. If you’re using a cloud-based hosting provider, the process may vary.

## Server Setup

Set up a server or hosting environment (e.g., AWS, DigitalOcean, or a VPS provider) with your preferred Linux distribution (e.g., Ubuntu, CentOS).

## Install NGINX

- Update your server’s package list: `sudo apt update` (for Ubuntu) or `sudo yum update` (for CentOS).

- Install Nginx: `sudo apt install nginx` (for Ubuntu) or `sudo yum install nginx` (for CentOS).

- Start and enable Nginx: `sudo systemctl start nginx` and `sudo systemctl enable nginx`.

## Install MySQL

- Install MySQL Server: `sudo apt install mysql-server` (for Ubuntu) or `sudo yum install mysql-server` (for CentOS).

-  Secure your MySQL installation by running `mysql_secure_installation` and following the prompts.

## Create a MySQL Database and User

- Log in to MySQL: `mysql -u root -p` .

- Create a new database for your WordPress site: `CREATE DATABASE your_db_name;` .

- Create a MySQL user and grant privileges to the database:

``` bash
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
```

- Exit MySQL: `EXIT;` .


## Install PHP-FPM

- Install PHP and required extensions: `sudo apt install php-fpm php-mysql` (for Ubuntu) or `sudo yum install php-fpm php-mysql` (for CentOS).

- Configure PHP-FPM settings if necessary.

## WordPress Installation

Download and extract WordPress to your web server root directory:

``` bash
cd /var/www/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
```

- Set the correct permissions:

``` bash
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
```

## Nginx Configuration

- Create an Nginx server block (Virtual Host) configuration for your WordPress site. You can create a new configuration file in `/etc/nginx/sites-available/` and symlink it to `/etc/nginx/sites-enabled/`. 
Here's a basic configuration:

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

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

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

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

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

- Test the configuration: `sudo nginx -t` .
- Reload Nginx: `sudo systemctl reload nginx` .

## WordPress Configuration

- Open your web browser and access your domain. You will see the WordPress setup wizard.
- Follow the instructions to set up your WordPress site, providing the database name, username, and password you created earlier.

## WordPress Plugins and Themes

- Customize your site by installing themes and plugins as needed.

## Secure Your Website

- Install an SSL certificate to enable HTTPS for your site.
- Implement security best practices, such as using strong passwords, disabling XML-RPC, and regularly updating WordPress and its plugins.


After completing these steps, your WordPress website should be up and running with Nginx and MySQL. Don’t forget to regularly update your WordPress installation, themes, and plugins to keep your website secure and functioning correctly.