WordPress 4.8 安装配置教程 ,基于 centos 7.3, php 7.0, mysql 5.7.19, nginx 1.12.1

最近想要整个 blog,记录自己工作、学习中的点滴。Wordpress 自然是首选,因为内容才是关键,所以也就不怕别人说太 low。网上大部份都是讲 wordpress 配合 apache 的安装教程。基于 nginx 的只有几篇比较老的,有些已经不太适用了。捣鼓了小半天,终于搞定,分享出来,也给需要的朋友一个参考。

一、下载 & 解压 wwordpress

先新建一个临时目录,用于存放各种临时的安装包,例如 ~/temp

1 mkdir ~/temp
2  
3 cd ~/temp

下载 wordpress

1 wget https://wordpress.org/latest.zip -O wordpress.zip

解压

1 unzip wordpress.zip

将解压后的文件夹移动到提供 web 服务器的目录下,例如 /var/www

1 sudo mv wordpress /var/www/

记住 /var/www/wordpress 这个目录,在第四步,安装 & 配置 nginx 的时候,我们将会用到它

二、安装 & 配置 mysql 5.7.19

访问:https://dev.mysql.com/downloads/repo/yum/

下载:mysql57-community-release-el7-11.noarch.rpm

1 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

执行

1 sudo rpm -ivh mysql57-community-release-el7-11.noarch.rpm

安装 mysql

1 sudo yum install mysql-community-server

安装完成之后,用下面的命令获取 mysql 自动生成的临时登陆密码

用这个临时登陆密码

1 mysql -u root -p

登陆 mysql

然后,依次执行下面的命令,修改登陆密码 (将 YOUR_PASSWORD 换成你想要设置的密码)

1 use mysql;
2  
3 update user set password=PASSWORD("YOUR_PASSWORD") where user="root";
4  
5 flush privileges;

创建 wordpress 数据库,并退出

1 CREATE DATABASE wordpress;
2  
3 quit;

记住数据库的名称 wordpress,以及你设置的 mysql 登陆密码,我们在第五步,安装 wordpress 的时候,需要用到它们。

三、安装 & 配置 php 7.0 & php-fpm

安装 PHP7 的 yum 源

1 sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装 php 7, php-fpm

1 sudo yum install php70w php70w-fpm php70w-mysql php70w-gd  php70w-mbstring

打开 php 配置文件

1 sudo vim /etc/php.ini

找到 cgi.fix_pathinfo,取消注释,并将其设置为 0

1 ; http://php.net/cgi.fix-pathinfo
2 cgi.fix_pathinfo=0

打开 php-fpm 配置文件

1 sudo vim /etc/php-fpm.d/www.conf

修改下面几处地方

 1 ; RPM: apache Choosed to be able to access some dir as httpd
 2 ;user = apache
 3 ; RPM: Keep a group allowed to write in log dir.
 4 ;group = apache
 5 user = nginx
 6 group = nginx
 7  
 8 ; '/path/to/unix/socket' - to listen on a unix socket.
 9 ; Note: This value is mandatory.
10 ;listen = 127.0.0.1:9000
11 listen = /var/run/php-fpm.sock
12  
13 ; Default Values: user and group are set as the running user
14 ; mode is set to 0660
15 listen.owner = nginx
16 listen.group = nginx

重启 php-fpm

1 sudo systemctl restart php-fpm

设置系统启动时自动运行 php-fpm

1 sudo systemctl enable php-fpm

记住 listen = /var/run/php-fpm.sock,第四步,安装 & 配置 nginx 的时候需要用到

四、安装 & 配置 nginx 1.12.1

新建 nginx 源文件

1 sudo vim /etc/yum.repos.d/nginx.repo

写入以下内容

1 [nginx]
2 name=nginx repo
3 baseurl=http://nginx.org/packages/centos/7/$basearch/
4 gpgcheck=0
5 enabled=1

安装 nginx

1 sudo yum install nginx

备份 nginx 配置文件

1 sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.back

打开 nginx 配置文件

1 sudo cp /etc/nginx/conf.d/default.conf

按如下方式修改配置

 1 server {
 2     listen 80;
 3     server_name www.YOUR_DOMAIN.com;
 4     # 将 YOU_DOMAIN 换成你自己的域名
 5  
 6     # /var/www/wordpress 与第一步,wordpress 的存放目录一致
 7     root /var/www/wordpress;
 8     index index.php index.html index.htm;
 9  
10     #charset koi8-r;
11     #access_log /var/log/nginx/host.access.log main;
12  
13     location / {
14         try_files $uri $uri/ /index.php?$args;
15     }
16  
17     error_page 404 /404.html;
18  
19     # redirect server error pages to the static page /50x.html
20     error_page 500 502 503 504 /50x.html;
21     location = /50x.html {
22         root /usr/share/nginx/html;
23     }
24  
25     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
26     location ~ \.php$ {
27         try_files $uri =404;
28         # /var/run/php-fpm.sock 要和第三步,php-fpm 的配置一致
29         fastcgi_pass unix:/var/run/php-fpm.sock;
30         fastcgi_index index.php;
31         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
32         include fastcgi_params;
33     }
34  
35     # deny access to .htaccess files, if Apache's document root
36     # concurs with nginx's one
37     #
38     #location ~ /\.ht {
39     # deny all;
40     #}
41 }

重启 nginx

1 sudo systemctl restart nginx

五、安装 wordpress

更改 wordpress 所有者为 nginx

1 sudo chown -R nginx:nginx /var/www/wordpress/

复制示例生成配置文件

1 sudo cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php

打开配置文件

1 sudo vim /var/www/wordpress/wp-config.php

填入第二步,设置的 mysql 数据库名称、用户名和密码

1 // ** MySQL settings - You can get this info from your web host ** //
2 /** The name of the database for WordPress */
3 define('DB_NAME', 'wordpress');
4  
5 /** MySQL database username */
6 define('DB_USER', 'root');
7  
8 /** MySQL database password */
9 define('DB_PASSWORD', 'YOUR_MYSQL_PASSWORD');

将 YOUR_MYSQL_PASSWD 替换成你自己的 mysql 登陆密码

访问 www.YOUR_DOMAIN.com/wp-admin/install.php

按照提示一步步开始安装 wordpress 啦!

【原文链接】http://www.ipaomi.com/2017/08/01/wordpress-4-8-安装配置教程-(基于-centos-7-3-php-7-0-mysql-5-7-19-nginx-1-12-1)/