How to configure wordpress with nginx

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that time I used a third-party virtual host, and there might be dozens of websites on one machine. At that time, virtual machines had not yet achieved physical isolation, and one website occupying resources could cause all websites to crash.

Recently, I tried to build an independent WordPress on Alibaba Cloud ECS and recorded the construction process.

Originally I wanted to try to use apache as the container for wordpress, and use nginx as a reverse proxy directly to apache. But after I used apache, I found a problem. When I used nginx directly as the reverse proxy and switched to apache, the pseudo-static URL seemed inaccessible. It seemed that I needed to configure the nginx location for each URL. It felt too troublesome, so I gave up.

Of course, there may be other solutions, but I didn't try them. Later, I checked and found that nginx can use third-party plug-ins to support PHP. Directly using nginx as a container also has advantages. It is relatively simple to deploy, and multiple websites can be deployed directly through a single nginx container.

1. Domain name purchase

I won’t go into details about this. Just go to the domain name platform and apply for a domain name.

2.MySQL installation

I have already talked about how to install MySQL. You can see the details here. After the installation is complete, we need to create a separate account for WordPress. Why do we need to create a separate account? This is mainly for security reasons. If WordPress is attacked and you use the root account, all tables in the database will be exposed.

// Create a wordpress user and set a password. It is recommended that the password be randomly generated and not less than 8 characters, using a combination of uppercase and lowercase letters, numbers, and special characters. CREATE USER 'wordpress'@'%' IDENTIFIED BY 'password';

//Give the wordpress account all operation permissions, including select delete update insert create alter, etc. GRANT all ON wordpress.* TO 'wordpress'@'%';

Detailed information about permissions can be found here.

3. nginx installation

Nginx is installed using yum, which is very simple.

Install:

yum -y install nginx;

start up:

systemctl start nginx.service;

Two steps complete the installation of nginx.

4. Install PHP

4.1 PHP installation

yum -y install php;

PHP installation is very simple. After the installation is complete, execute the following command to view the version, which means the installation is correct:

php -v;

4.2 php-fpm installation

In addition to PHP, we also need two things, fast-cgi and php-fpm. So what are these two things? If you want to know more details, you can see here. Simply put, fpm is a manager of fastcgi. Before this, I didn't know that I needed to install fpm to correctly parse php files, and I struggled for a long time.

yum install php-fpm;

//Check whether the installation is successful php-fpm -v;

Start php-fpm 
systemctl start php-fpm;

By default, fpm occupies port 9000.

5. Install WordPress

wget https://wordpress.org/latest.tar.gz;

//Unzip tar -xzf latest.tar.gz -C /var/www/html;

After decompression, find the :/wordpress/wp-config-sample.php file and modify the database name, username, and password. The fields are as follows:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'Your database name');

/** MySQL database username */
define('DB_USER', 'Your username');

/** MySQL database password */
define('DB_PASSWORD', 'Your password');

/** MySQL hostname */
define('DB_HOST', 'your host');

After the modification is completed, change the file name of wp-config-sample.php to: wp-config.php.

6. Configure nginx

The following is my configuration, you can refer to:

# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

upstream php {
 #server unix:/tmp/php-cgi.socket;
 #Point to the default port 9000 of fpm,
 server 127.0.0.1:9000;
}

server { 
 listen 80 ;
 listen [::]:80 ;
 server_name www.domain.com;
 root /web/www.domain.com/;
 index index.php;
 
 location ~ \.php$ {
  #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  include fastcgi.conf;
  fastcgi_intercept_errors on;
  fastcgi_pass php;
 }

 # Load configuration files for the default server block.
 include /etc/nginx/default.d/*.conf;
 }

7. Security Settings

About 20% of the websites in the world use the WordPress system, which has also made WordPress a target of hacker attacks. Security issues cannot be ignored, whether for individuals or businesses. Is there any simple way for ordinary users to quickly improve security protection? I checked out several security plugins and found one that can help us improve our security strategy. It’s called All In One WP Security & Firewall. This plug-in is relatively simple and easy to use for ordinary users.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Six steps to teach you how to build a personal blog based on WordPress from scratch
  • Detailed explanation of the whole process of building a personal blog with nginx+WordPress
  • Detailed steps for setting up host Nginx + Docker WordPress Mysql
  • Detailed explanation of WordPress multi-site configuration under Nginx environment
  • Example of how to set WordPress pseudo-static in Nginx
  • Detailed graphic tutorial on how to solve the slow access problem of WordPress website

<<:  MySQL 5.7 and above version download and installation graphic tutorial

>>:  Mysql5.7 service cannot be started. Graphical solution tutorial

Recommend

JavaScript implementation of drop-down list

This article example shares the specific code of ...

Record of the actual process of packaging and deployment of Vue project

Table of contents Preface 1. Preparation - Server...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

HTML Learning Notes--Detailed Explanation of HTML Syntax (Must Read)

1. What is HTML markup language? HTML is a markup...

Using HTML web page examples to explain the meaning of the head area code

Use examples to familiarize yourself with the mean...

Detailed explanation of the difference between $router and $route in Vue

We usually use routing in vue projects, and vue-r...

A graphic tutorial on how to install MySQL in Windows

Abstract: This article mainly explains how to ins...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

A brief analysis of controlled and uncontrolled components in React

Table of contents Uncontrolled components Control...

The difference between key and index in MySQL

Let's look at the code first: ALTER TABLE rep...

Implementing a puzzle game with js

This article shares the specific code of js to im...

Analyzing the node event loop and message queue

Table of contents What is async? Why do we need a...

Linux uses NetworkManager to randomly generate your MAC address

Nowadays, whether you are on the sofa at home or ...