Mud: Use nginx to build a Wordpress blog using a virtual host

Source: Internet
Author: User
Tags install wordpress wordpress blog

This article was sponsored by Xiuyi linfeng and first launched in the dark world.

Recently, I plan to learn about nginx Web servers. Since it is learning or practical. We will build a Wordpress blog here as an example.

To build a Wordpress blog, we need the MySQL database, PHP environment, and nginx web server. The procedure is as follows:

1. MySQL database installation and configuration

2. installation and configuration of PHP, PHP-FPM and related PHP Components

3. nginx Web Server construction and Configuration

4. install Wordpress

Note: In this experiment, apart from installing nginx using the source code, all other software is installed using the RPM package yum.

I,MySQLDatabase installation and configuration

1.1 MySQLDatabase Installation

MySQL database, we use yum for installation. As follows:

Yum-y install MySQL mysql-Server

1.2Modify the MySQL Database Root User Password and configure Remote Access Permissions

After the MySQL database is installed, start the MySQL database, modify the settings of the MySQL database, and create the ailanni database for Wordpress. As follows:

/Etc/init. d/mysqld start

Modify the password of the root user of the MySQL database. The default password of the root user is blank after the MySQL database is installed. Run the following command:

Mysql-u root-P or MySQL-uroot-P

Use MySQL;

Update user SET Password = PASSWORD ('000000') where user = 'root ';

Flush privileges;

/Etc/init. d/mysqld restart

By default, the MySQL database only allows local access to the MySQL database. However, for the convenience of MySQL database management, we need to enable the remote access permission for MySQL database. Run the following command:

Mysql-u root-P or MySQL-uroot-P

Use MySQL;

Update user set host = '%' where user = 'root ';

Flush privileges;

/Etc/init. d/mysqld restart

After the above configuration is complete, you will find that the MySQL connection is very slow. You only need to modify the configuration file of the MySQL database.

Open the/etc/My. CNF file, add skip-name-resolve to mysqld, and then restart the MySQL database. As follows:

VI/etc/My. CNF

/Etc/init. d/mysqld restart

1.3Create a database for WordPress

After the preceding operations, we can connect to the MySQL database. Here we use the navicat premium database client.

Use navicat premium to create the database required by the Wordpress blog. For details about how to use navicat premium to create a MySQL database, refer to "mud: navicat premium to use MySQL Database". After the database is created, it is as follows:

Of course, we can also use phpMyAdmin to create the required database for Wordpress.

For installing and using phpMyAdmin on nginx, I will introduce it in the next article.

II,PHP,PHP-FPMAndPHPInstallation and configuration of related components

After the MySQL database is installed, configure the PHP environment.

2.1Install PHP and related components

First install PHP and related components and run the following command:

Yum-y install PHP *

In related components of PHP, we must pay attention to the two components: PHP-MySQL and PHP-FPM. If you use PHP-MYSQL to connect to MySQL, you will be prompted when installing WordPress: Your PHP does not seem to have installed the MySQL extension necessary to run WordPress. After installation, restart the MySQL database. Otherwise, WordPress cannot connect to the MySQL database.

While PHP-FPM is used in combination with nag.pdf, nginx itself does not support PHP, and PHP language is resolved through PHP-FPM and nginx.

The two components can be installed separately as follows:

Yum-y install PHP-MySQL

Yum-y install PHP-FPM

After the above installation is complete, let's check the PHP version. As follows:

Php-V

2.2Configure PHP-FPM

After installing PHP-related components, we need to configure PHP-FPM.

View the related installation documents of PHP-FPM as follows:

Rpm-ql php-FPM

We can see the related configuration file of PHP-FPM. The main configuration file is/etc/php-fpm.conf, now view the configuration file, as follows:

More/etc/php-fpm.conf

You will find that the configuration file is not defined at all. The port to be listened to and the address to be listened to by PHP-FPM.

Why do we say the analysis is incorrect?

In fact, there is nothing wrong with our analysis. Note, we marked the part.

Include =/etc/php-fpm.d/*. conf

This statement is very important and contains the key part of the configuration in PHP-FPM. The key configuration file is the www. conf file marked in the PHP-FPM installation diagram.

// Etc/php-fpm.d/www. conf

Now let's check the file, as shown below:

More/etc/php-fpm.d/www. conf

In this figure, the marked part shows the address and port to be listened to by PHP-FPM.

This figure shows the users and user groups used to run the PHP-FPM process. Here, we use the default user and User Group Apache.

To put it bluntly, if we have installed PHP-FPM without any configuration, use the default configuration.

Of course, if you want to further optimize the PHP-FPM configuration, you can use the configuration template provided during the PHP-FPM installation. The configuration template is located:

More/usr/share/doc/php-fpm-5.3.3/php-fpm.conf.default

After the above configuration is complete, start PHP-FPM and check whether the listening port and address are normal. As follows:

/Etc/init. d/PHP-FPM start

Chkconfig PHP-FPM on

Netstat-tunlp | grep 9000

PS aux | grep PHP-FPM

Through this, we can see that PHP-FPM is currently running normally. The listening address and port are 127.0.0.1: 9000, and PHP-FPM is run by Apache users. The main process is running as the root user.

Through the PHP-FPM startup method, we can see that PHP-FPM is running in the service mode. Then we can restart and disable PHP-FPM through the server. As follows:

/Etc/init. d/PHP-FPM restart

/Etc/init. d/PHP-FPM stop

III,Nginx WebServer construction and Configuration

3.1InstallNginx

Upload the downloaded nginx file to the server and run the following command:

SCP tengine-2.0.3.tar.gz [email protected]:/tmp

Ifconfig eth0 | grep "Inet ADDR" | awk '{print $2}' | cut-D:-F2

Decompress nginx and compile and install nginx as follows:

Tar-XF/tmp/tengine-2.0.3.tar.gz-C ./

./Configure

Make & make install

3.2Start nginx

After nginx is installed, start nginx. Nginx can be started in two ways.

One is as follows:

/Usr/local/nginx/sbin/nginx

Another method is as follows:

/Usr/local/nginx/sbin/nginx-C/usr/local/nginx/CONF/nginx. conf

In fact, the first method is to load the/usr/local/nginx/CONF/nginx. conf configuration file by default.

We can see it clearly. Nginx has been started and can be accessed normally.

Now let's look at the users running nginx, as follows:

PS-Aux | grep nginx

We can see that nginx is currently running under the nobody user.

3.3Configure nginx to support PHP

After nginx is installed, We need to configure nginx to support PHP, and we also need to configure nginx for Wordpress to be installed as a virtual host.

Configure the PHP options first.

Remove all comments related to FastCGI in the nginx configuration file. As follows:

VI/usr/local/nginx/CONF/nginx. conf

Location ~ \. Php $ {

Root HTML;

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Fastcgi_param script_filename$ Document_root$ Fastcgi_script_name;

Include fastcgi_params;

}

After the above configuration is complete, we will configure the homepage for nginx. Add the index. php file as follows:

Location /{

Root HTML;

Index index. php index.html index.htm;

}

Now let's test whether the nginx PHP configuration is correct. Edit the index. php file and add <? PHP phpinfo () ;?>, As follows:

Vi./html/index. php

Run the following command to test whether the nginx configuration is correct and then restart nginx elegantly:

/Usr/local/nginx/sbin/nginx-T

/Usr/local/nginx/sbin/nginx-s reload

We can see that nginx currently supports PHP normally.

3.4Configure nginx to support Virtual Hosts

After configuring nginx for PHP, We will configure the nginx virtual host.

In fact, nginx configuration is very simple, the entire configuration file. There is only one HTTP tag, and the virtual host configuration is one server tag.

However, the server tag must be included in the HTTP tag. The purpose is that the server tag can exist either in the configuration file where HTTP is located or in a separate file, however, you need to use include in the HTTP tag.

Below is a virtual host a.ilanni.com I configured, as shown below:

VI/usr/local/nginx/CONF/nginx. conf

Server {

Listen 80;

SERVER_NAME a.ilanni.com;

Root/ilanni/a.ilanni.com /;

Index index. php;

Location ~ \. Php $ {

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Fastcgi_param script_filename$ Document_root$ Fastcgi_script_name;

Include fastcgi_params;

}

}

Note: I am storing the root directory of the.ilanni.com virtual host in the directory/ilanni/a.ilanni.com/and the user group of the virtual host is root.for example:

To make a difference from the default nginx homepage, I now use a PHP probe for the home page of the VM a.ilanni.com.

After configuring the VM, restart nginx as follows:

/Usr/local/nginx/sbin/nginx-T

/Usr/local/nginx/sbin/nginx-s reload

We can see it clearly. Currently, the VM a.ilanni.com can be accessed normally and the PHP probe I mentioned is displayed.

IV,WordPressInstall

We have installed WordPress above. All the MySQL Databases and nginx Web servers required have been set up.

Now we will upload the WordPress installation package to the server for decompression as follows:

SCP wordpress-4.0-zh_CN.tar.gz [email protected]:/ilanni

Tar-XF wordpress-4.0-zh_CN.tar.gz

Copy the extracted file to the root directory a.ilanni.com of the VM, as shown below:

CP-RV./WordPress a.ilanni.com/

Start to install Wordpress and access a.ilanni.com in the browser as follows:

Enter information about the MySQL database. As follows:

Copy this information to the file wp-config.php. If the file does not exist, create a wp-config.php file under the.ilanni.com root directory, as shown below:

VI wp-config.php

Set the Sydney type of the blog as follows:

The prompt is as follows:

Access the home page of the VM a.ilanni.com as follows:

The installation of the Wordpress blog under nginx has been completed.

Mud: Use nginx to build a Wordpress blog using a virtual host

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.