心血来潮,想用nginx+wordpress搭建一个简单的个人站,结果遇到了很多坑。在这里将心路历程做一个简单的记录。
服务器:阿里云cnetos7.8
数据库:mariadb
nginx:1.17.3(源码安装)
1、安装mariadb:
yum -y install mariadb mariadb-server mariadb-devel
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation # mariadb初始化脚本
mysql -uroot -p
> create database wordpress;
2、安装php
# 在这里就开始遇到坑了
# yum提供的php是5.4坂本的,但是我下载的wordpress需要至少5.6.20以上的支持
# 因此要对php进行升级,升级为php7
yum -y remove php* # 卸载php相关软件包
# 修改对应的yum源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum search php7 # 查看yum中是否包含php7相关包
# 安装相关插件,这里可以自行选择
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
3、配置wordpress
mv /srv/wordpress /usr/local/nginx/html/ # 将wordpress项目移动到nginx页面目录下
cd /usr/local/nginx/html/wordpress
cp wp-config-sample.php wp-config.php
vim wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define( 'DB_NAME', 'wordpress' ); # 上面在mariadb创建的数据库
/** MySQL数据库用户名 */
define( 'DB_USER', 'root' );
/** MySQL数据库密码 */
define( 'DB_PASSWORD', '123456' );
/** MySQL主机 */
define( 'DB_HOST', 'localhost' );
/** 创建数据表时默认的文字编码 */
define( 'DB_CHARSET', 'utf8' );
# 搭建完成,运行项目的时候遇到的坑
# 还有插件安装需要FTP认证问题的解决
# 用户安装插件权限问题的解决(chown -R apache.apache wp-content/,不同的服务器的用户可能不一样,我服务器的用户是apache)
4、安装并配置nginx
# 安装
yum -y install gcc pcre-devel openssl-devel # nginx相关依赖包
useradd -s /sbin/nologin nginx # 创建一个不能登录的用户用来启动Nginx
tar -zxvf nginx.1.17.3.tar.gz
cd ./nginx.1.17.3
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
make && make install
# 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak # 配置文件备份
vim nginx.conf # 在配置的时候遇到了坑
...
location / { # 坑在这里,为了加载图片等静态资源
# static resources
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|js|css|woff|ttf)$ {
root html/wordpress;
expires 7d;
}
# default page
location / { # 默认页面
root html/wordpress;
index index.php;
}
}
location ~ \.php$ {
root html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
提高访问速度我使用的是WP Super Cache插件。目前就这么多东西,之后用到一些别的东西会进行补充的,有问题希望大家指出。
最后给一个项目成功运行的截图。
补充:
解决wordpress安装主题或插件需要FTP问题
解决Wordpress不能自动安装主题、插件
遇到了问题:
解决方法:将PHP升级到7.4就可以解决了,并且wordpress的访问速度有所提高。
又踩坑了:
把服务从一台机器上迁移到另外一台机器上,结果发现css样式无法加载,找了好大一波才找到解决办法,我是直接修改的mysql数据库,方法如下。