linux+nginx+mysql+php+wordpress
注意:如果你完全理解了,可以按照自己的思路进行,否则请按照我的版本和环境执行。
一、linux
本文中使用的是CentOS 7.8版本,理论上7.x都可以。
安装必要的依赖包
yum install gcc-c++ openssl-devel cyrus-sasl-md5 bzip2-devel curl-devel freetype-devel libjpeg-devel libpng-devel libxslt-devel libxml2-devel pcre-devel zlib-devel sqlite-devel
二、安装并配置nginx-1.18
1、编译安装nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make -j4 && make install
2、添加nginx用户和用户组
groupadd nginx
useradd -g nginx -s /sbin/nologin -M nginx
# -g:指定所属的group -s:指定shell,因为它不需要登录,所以用/sbin/nologin -M:不创建home目录,因为它不需要登录
3、配置nginx.conf
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; # 指定用户和用户组
error_log logs/error.log; # 放开日志,同时需要放开http{}里面的log_format
pid logs/nginx.pid; # 指定pid文件
3.1、配置http{ }
# 放开日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 在上传wordpress或者图片的时候,如果不增加配置,文件过大容易出现 413 Request Entity Too Large这个错误
client_max_body_size 20M;
3.2、配置server{ }
主要修改location{},追加index.php让nginx服务器默认支持index.php为首页:
location / {
root html;
index index.php index.html index.htm;
}
然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:
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;
}
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root修改客户端参数
4、添加服务化
编辑nginx.service文件,将命令目录地址修改为实际地址
vim /usr/lib/systemd/system/nginx.service
修改后的内容如下:
[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载服务文件,并启动nginx,最后在浏览器访ip地址测试nginx是否运行正常
systemctl daemon-reload
systemctl start nginx
5、添加开机启动
#第一种方法(推荐)
systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
#第二种方法
vim /etc/rc.local
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &
chmod +x /etc/rc.d/rc.local
6、配置环境变量(可选)
vim /etc/profile
for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
# 在这里加入环境变量
export PATH="$PATH:/usr/local/nginx/sbin:"
unset i
unset -f pathmunge
三、安装配置MySQL
1、下载安装MySQL5.7
yum -y install mysql57-community-release-el7-10.noarch.rpm
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
2、启动服务
systemctl start mysqld
systemctl status mysqld
3、修改默认密码,并登陆
grep "password" /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
set global validate_password_policy=LOW;
set global validate_password_length=6;
SHOW VARIABLES LIKE 'validate_password%';
4、开启远程访问权限(可选)
grant all privileges on *.* to 'root'@'your ip' identified by 'your password' with grant option;
flush privileges;
5、配置防火墙
systemctl start firewalld
firewall-cmd --zone=public --add-port=3306/tcp --permanent # 开放3306端口
firewall-cmd --zone=public --remove-port=3306/tcp --permanent #关闭3306端口
firewall-cmd --reload # 配置立即生效
查看防火墙
firewall-cmd --zone=public --list-ports #查看防火墙所有开放的端口
6、修改Server端字符集
vim /etc/my.cnf
[client]
default-character-set=utf8mb4
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
四、安装配置PHP(需要的时间较长)
1、安装正则表达式库
wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.5+rev1-4.el7.remi.x86_64.rpm
rpm -Uvh oniguruma5php-6.9.5+rev1-4.el7.remi.x86_64.rpm
wget https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.5+rev1-4.el7.remi.x86_64.rpm
rpm -Uvh oniguruma5php-devel-6.9.5+rev1-4.el7.remi.x86_64.rpm
2、下载php-7.4.11
wget https://www.php.net/distributions/php-7.4.11.tar.gz
tar zxf php-7.4.11.tar.gz
cd php-7.4.11
3、配置并检查依赖
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-libdir=lib64 --with-curl --enable-gd --with-gettext --with-iconv-dir --with-kerberos --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --without-pear --disable-phar
4、编译安装
由于时间较长,可以使用多任务编译。4核主频4.3Ghz的处理器实测结果:1job耗时6分16秒,2jobs耗时3分20秒,4jobs耗时2分15秒,8jobs耗时2分13秒。
make -j4
make install
# 结果
Installing build environment: /usr/local/php/lib/php/build/
Installing helper programs: /usr/local/php/bin/
program: phpize
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
program: php-config
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing phpdbg binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP FPM defconfig: /usr/local/php/etc/
Installing phpdbg man page: /usr/local/php/php/man/man1/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
Installing header files: /usr/local/php/include/php/
5、配置环境变量
vim /etc/profile
#在最后添加
PATH=$PATH:/usr/local/php/bin
export PATH
#更新环境变量
source /etc/profile
php -v #查看版本
6、配置php-fpm
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 不再使用service控制,改为systemctl控制
#cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#chmod +x /etc/init.d/php-fpm
7、配置systemctl控制
新建php-fpm.service文件
vim /usr/lib/systemd/system/php-fpm.service
追加下面的内容
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
重新加载服务文件,并启动nginx,最后在浏览器访ip地址测试nginx是否运行正常
systemctl daemon-reload
systemctl enable php-fpm
systemctl start php-fpm
8、测试运行
在nginx的html目录写一个test.php页面进行测试
<?php
phpinfo();
?>
五、安装wordpress
1、下载wordpress-5.5.1
wget https://wordpress.org/latest.tar.gz
tar zxf latest.tar.gz
2、创建www用户及用户组
groupadd www
useradd -g www -s /sbin/nologin -M www
3、修改WP上传文件参数
需要上传主题的时候,对内存有限制,所以需要手动调整到合适的大小。去修改/usr/local/php/etc/php.ini
upload_max_filesize = 20M # 最大文件大小
max_file_uploads = 200 #最大文件数
4、解决需要FTP的问题(如果没出现则忽略)
进入wordpress的wp-content目录,新建tmp文件夹,设置文件夹的权限为777,并设置wp-content目录中的plugins(插件)和themes(主题)文件夹权限为777pwd
在wp-config.php中
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
的后面添加:
define('WP_TEMP_DIR', ABSPATH.'wp-content/tmp');
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
5、编译安装GD图片处理模块
# 进入PHP的源码扩展目录
cd /data/php-7.4.11/ext/gd
# 在该目录下执行phpize
/usr/local/php/bin/phpize
# 配置
./configure --with-php-config=/usr/local/php/bin/php-config
# 编译
make
# 安装
make install
# 在php.ini文件中添加
extension=gd.so
# 重启php
service php-fpm restart
本文完