CentOS7环境下WordPress搭建部署

Hi, Everyone!Welcome to DQF's Blog,For technical exchange, Please add QQ: 905030209,Thanks!

目录

环境说明

查看系统信息

[root@ctos7 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@ctos7 ~]# uname -a
Linux ctos7.6 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@ctos7 ~]# getenforce 
Disabled
[root@ctos7 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@ctos7 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G  4.4G   34G  12% /
devtmpfs       devtmpfs  486M     0  486M   0% /dev
tmpfs          tmpfs     496M     0  496M   0% /dev/shm
tmpfs          tmpfs     496M  492K  496M   1% /run
tmpfs          tmpfs     496M     0  496M   0% /sys/fs/cgroup
tmpfs          tmpfs     100M     0  100M   0% /run/user/1000
[root@ctos7 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           991M        431M        474M        496K         84M        441M
Swap:            0B          0B          0B

为防止编译出错,增加swap空间

mkdir /opt/images/
dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
chmod 0600 /opt/images/swap
mkswap /opt/images/swap
swapon /opt/images/swap
free -h

内存太小,增加内存可以解决,使用完毕后可以关掉swap:

swapoff swap
rm -f /opt/images/swap

LNMP环境搭建

部署Nginx

安装Nginx依赖包

yum install -y pcre-devel openssl-devel

pcre:兼容perl语言正则表达式,perl compatible regular expressions
rewirte模块 参数信息(perl方式定义正则表达式)
openssl:ssh—openssh/openssl—https

下载nginx软件

cd /data/tools
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xvf nginx-1.16.0.tar.gz

创建管理用户www

useradd -M -s /sbin/nologin www

编译nginx

配置软件,在软件的解压目录中

cd /data/tools/nginx-1.16.0
./configure --prefix=/usr/local/nginx-1.16.0 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
echo $?

–prefix 表示指定软件安装到哪个目录中,指定目录不存在会自动创建
–user/–group nginx工作进程由哪个用户运行管理
–with-http_stub_status_module 启动nginx状态模块功能(用户访问nginx的网络信息)
–with-http_ssl_module 启动https功能模块
通过软件编译过程中的返回值是否正确,确认配置是否正确

编译安装软件

make && make install

创建软链接

ln -s /usr/local/nginx-1.16.0/ /usr/local/nginx

精简化nginx.conf 主配置文件内容, 编写nginx配置文件

cp /usr/local/nginx/conf/nginx.conf{,.bak}
cd /usr/local/nginx/conf/
egrep -v "#|^$" nginx.conf.default >nginx.conf

启动nginx程序

/usr/local/nginx/sbin/nginx

检查nginx进程及端口

ps -ef |grep nginx
netstat -lntup |grep 80

进行访问测试

从浏览器中访问主机地址或域名,如www.ddstudy.site

编辑nginx启动脚本

cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

部署MySQL

下载Mysql yum repository并安装mysql-server

MySQL官方下载链接
MySQL FTP下载

cd /data/tools
wget https://dev.mysql.com/downloads/file/?id=484922
rpm -Uvh mysql80-community-release-el7-3.noarch.rpm 
yum repolist all | grep mysql
yum install -y mysql-community-server

启动mysql并检查启动状态后设置开机自启动

systemctl start mysqld.service
systemctl status mysqld.service
netstat -lntup|grep 3306
ps -ef|grep mysqld
systemctl enable mysqld.service

查看’root’@’localhost’用户密码

grep 'temporary password' /var/log/mysqld.log

更改’root’@’localhost’用户密码

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'cm9vdA==';
CREATE  USER 'root'@'%' IDENTIFIED BY 'cm9vdA==';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

查看mysql其他可用组件

yum --disablerepo=\* --enablerepo='mysql*-community*' list available

部署PHP

安装PHP依赖包

yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

安装libiconv软件 和字符集转换相关软件

cd /data/tools
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
tar xvf libiconv-1.16.tar.gz
cd libiconv-1.16/
./configure --prefix=/usr/local/libiconv
make && make install

fpm 定制rpm包

rpm包制作软件—把编译后的程序目录进行打包,通过fpm相关参数指定rpm解压之前要先安装哪些依赖

yum -y install libmcrypt-devel mhash mcrypt
rpm -qa libmcrypt-devel mhash mcrypt

下载php源码包

cd /data/tools
wget https://www.php.net/distributions/php-7.3.7.tar.gz

编译安装PHP

cd /data/tools
tar xvf php-7.3.7.tar.gz
cd php-7.3.7/
./configure \
--prefix=/usr/local/php-7.3.7 \
--with-config-file-path=/usr/local/php-7.3.7 \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pear \
--with-freetype-dir=/usr \
--with-zlib \
--with-libxml-dir=/usr \
--with-xmlrpc \
--with-curl \
--with-iconv-dir=/usr/local/libiconv \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--enable-cli \
--with-xsl \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-ftp \
--enable-opcache=no
echo $?
make && make install
echo $?

PHP软件程序创建软链接

ln -s /usr/local/php-7.3.7/ /usr/local/php

配置php解析文件/配置php-fpm配置文件

配置文件说明:
   php.ini-developments是开发人员调试用配置文件
   php.ini-production是生产常见所有配置文件
文件区别对比:
生产的文件不会输出过多的日志信息,而开发文件会输出大量程序测试日志信息

ll /data/tools/php-7.3.7/php.ini*
cp /data/tools/php-7.3.7/php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf

启动php-fpm程序并检查启动状态

/usr/local/php/sbin/php-fpm
netstat -lntup |grep 9000

编辑php启动脚本

cat  /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
PrivateTmp=true

[Install]
WantedBy=multi-user.target

nginx与php建立连接

编辑nginx与php连通性测试文件,并进行测试

mkdir /usr/local/nginx/html/blog/
echo '<?php phpinfo(); ?>' >/usr/local/nginx/html/blog/test_info.php
echo 'This is a test' >/usr/local/nginx/html/blog/index.html

修改nginx配置文件,使nginx程序与php程序建立联系

mkdir /usr/local/nginx/conf/extra/
vim /usr/local/nginx/conf/extra/blog.conf
server {
        listen       80;
        server_name  www.ddstudy.site;
        location / {
                    root   html/blog;
                    index  index.php index.html index.htm;       
        }
        location ~* .*\.(php|php5)?$ {
                    root html/blog;
                    fastcgi_pass  127.0.0.1:9000;
                    fastcgi_index index.php;
                    include fastcgi.conf;
        }
}

配置nginx.conf

[root@ctos7 ~]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    include /usr/local/nginx/conf/extra/*.conf;
}

重新启动nginx

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s stop 
/usr/local/nginx/sbin/nginx

访问测试

curl http://blog.ddstudy.site/index.html
curl http://blog.ddstudy.site/test_info.php

mysql与nginx建立连接

创建WordPress数据库

mysql -uroot -p
CREATE DATABASE wordpress;
CREATE USER 'wordpress'@'172.26.122.%' IDENTIFIED BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'172.26.122.%';
CREATE USER 'wordpress'@'47.92.167.%' IDENTIFIED BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'47.92.167.%';
CREATE USER 'wordpress'@'%' IDENTIFIED BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'%';
CREATE USER 'wordpress'@'localhost' IDENTIFIED WITH mysql_native_password BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'localhost'; 
CREATE USER 'wordpress'@'47.92.167.%' IDENTIFIED WITH mysql_native_password BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'47.92.167.%'; 
SELECT user,host FROM mysql.user;
FLUSH PRIVILEGES;

编辑php与mysql连通性测试文件,并进行测试

vim /usr/local/nginx/html/blog/test_mysql.php
<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=wordpress;host=127.0.0.1';
$user = 'wordpress';
$password = 'd29yZHByZXNz=';
try {
    $dbh = new PDO($dsn, $user, $password);
    echo 'Mysql Connection Success !';
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

访问测试

http://47.92.167.250/test_mysql.php
curl http://47.92.167.250/test_mysql.php

部署WordPress

下载并解压WordPress

cd /data/tools
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar xvf latest-zh_CN.tar.gz

将WordPress文件拷贝到html/blog下

cp -r /data/tools/wordpress/* /usr/local/nginx/html/blog/
ll /usr/local/nginx/html/blog/
chown -R www.www /usr/local/nginx/html/blog/

安装WordPress

编辑wp-config.php配置文件

cd /usr/local/nginx/html/blog
cp -a wp-config-sample.php wp-config.php

访问网站进行初始化操作

问题归纳

问题展示-1

config.status: executing default commands
configure: WARNING: unrecognized options: --with-mysql, --with-mcrypt, --enable-gd-native-ttf

问题展示-2

virtual memory exhausted: Cannot allocate memory

解决方法

问题展示-3

[root@ctos7 etc]# /usr/local/php/sbin/php-fpm
[29-Jul-2019 22:05:33] WARNING: Nothing matches the include pattern '/usr/local/php-7.3.7/etc/php-fpm.d/*.conf' from /usr/local/php-7.3.7/etc/php-fpm.conf at line 143.
[29-Jul-2019 22:05:33] ERROR: No pool defined. at least one pool section must be specified in config file
[29-Jul-2019 22:05:33] ERROR: failed to post process the configuration
[29-Jul-2019 22:05:33] ERROR: FPM initialization failed
[root@ctos7 etc]# cd php-fpm.d/
[root@ctos7 php-fpm.d]# pwd
/usr/local/php/etc/php-fpm.d
[root@ctos7 php-fpm.d]# cd /usr/local/php/etc/php-fpm.d
[root@ctos7 php-fpm.d]# cd ..
[root@ctos7 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

解决方法

问题展示-4

[root@ctos7 blog]# /usr/local/php/bin/php test_mysql1.php 
Connection failed: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client[root@ctos7 blog]# 
mysql -uroot -p
CREATE USER 'wordpress'@'%' IDENTIFIED WITH mysql_native_password BY 'd29yZHByZXNz=';
ALTER USER 'wordpress'@'%' IDENTIFIED WITH mysql_native_password;
systemctl restart mysqld

解决方法
解决方法

问题展示-5

[root@ctos7 blog]# /usr/local/php/bin/php test_mysql1.php 
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'wordpress'@'localhost' (using password: YES)
CREATE USER 'wordpress'@'localhost' IDENTIFIED WITH mysql_native_password BY 'd29yZHByZXNz=';
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'localhost'; 

PHP编译参数说明

1 ./configure 编译参数
 2 
 3 –prefix=/application/php5.3.27 指定php的安装路径为/application/php5.3.27
 4 
 5 –with-mysql=/application/mysql/  #php7后取消此参数,用--with-mysqli替代
 6 需要指定mysql的安装路径,安装PHP需要的MySQL相关内容。当然如果没有MySQL软件包,也可以不单独安装,这样的情况可使用–with-mysql=mysqlnd替代–with-mysql=/application/mysql,因为PHP软件里面已经自带连接MySQL的客户端工具。    
 7 
 8 –with-iconv-dir=/usr/local/libiconv    libiconv库,各种字符集间的转换
 9 
10 –with-freetype-dir    打开对freetype字体库支持
11 
12 –with-jpeg-dir 打开对jpeg图片的支持
13 
14 –with-png-dir 打开对png图片的支持
15 
16 –with-zlib 打开zlib库的支持,用于http压缩传输
17 
18 –with-libxml-dir=/usr 打开libxml2库的支持
19 
20 –enable-xml    
21 
22 –disable-rpath 关闭额外的运行库文件
23 
24 –enable-safe-mode 打开安全模式
25 
26 –enable-bcmath 打开图片大小调整,用zabbix监控时会用到该模块
27 
28 –enable-shmop 
29 
30 –enable-sysvsem 使用sysv信号机制,则打开此选项
31 
32 –enable-inline-optimization 优化线程
33 
34 –with-curl 打开curl浏览工具的支持
35 
36 –with-curlwrappers 运维curl工具打开url流
37 
38 –enable-mbregex     
39 
40 –enable-mbstring 支持mbstring
41 
42 –with-mcrypt 编码函数库
43 
44 –with-gd 打开gd库的支持
45 
46 –enable-gd-native-ttf 支持TrueType字符串函数库
47 
48 –with-openl openl的支持,加密传输时用到
49 
50 –with-mhash mhash算法的扩展
51 
52 –enable-pcntl freeTDS需要用到,可能是链接mql
53 
54 –enable-sockets 打开sockets支持
55 
56 –with-xmlrpc 打开xml-rpc的c语言
57 
58 –enable-zip 打开对zip的支持
59 
60 –enable-soap soap模块的扩展
61 
62 –enable-short-tags 开始和标记函数
63 
64 –enable-zend-multibyte 支持zend的多字节
65 
66 –enable-static 生成静态链接库
67 
68 –with-xsl 打开XSLT文件支持,扩展libXML2库,需要libxslt软件
69 
70 –enable-ftp    打开ftp的支持
71 
72 –enable-fpm    表示激活PHP-FPM方式服务,即FactCGI方式运行PHP服务。
73 
74 –with-fpm-user=www    指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一。
75 
76 –with-fpm-group=www    指定PHP-FPM进程管理用户组为www,此处最好和Nginx服务用户组统一。

文献资料

https://www.php.net/manual/zh/configure.about.php
Leave a Reply

发表评论

电子邮件地址不会被公开。 必填项已用*标注

WordPress Theme: FreeDom by DQF.