WordPresss是用PHP开发的博客平台,对于新手来说不需要太多的时间也不需要太多的精力就可以搭建一个令人满意的个人博客。本文主要介绍CentOS 7下的WordPress环境搭建以及一些问题的解决方案。
在终端输入下列命令可以自动安装ngixn
yum -y install nginx
查看nginx版本信息
nginx -v
可以正确显示出版本信息则安装成功
接下来需要修改nginx的配置文件,配置文件的默认位置在/etc/nginx/nginx.conf,现在执行cd /etc/nginx
命令进入配置文件所在的文件夹。
vim nginx.conf
找到下面这部分,当然如果之后选择https协议这部分建议可以不用修改,因为之后还要重新对nginx.conf进行配置。
server {
listen 80 default_server;#默认使用80端口
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;#此处显示的是网站根目录可以修改
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
###########添加下面这段话###########
#下面这段话与读写权限有关,如果不添加Wordpress后台不能进行上传或下载
root /usr/share/nginx;
index index.html index.htm index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
#####################################
}
##############添加下面这句话#############
location ~ \.php$ { #解析php
root /usr/share/nginx;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx$fastcgi_script_name;
include fastcgi_params;
}
######################################
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
保存并退出即可
此时重启会报错
由于linux的yum源不存在php7.x,所以我们要更改yum源,并使用yum安装,直接yum install php
得到的是php5.x ,在这里建议使用php7.x,本案例安装的为php7.1 ,根据需要也可以安装php7.2:
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 php71w
yum 安装php71w和各种拓展,选自己需要的即可。
yum -y install php71w
yum install php71w-cli php71w-common php71w-devel php71w-embedded php71w-fpm php71w-gd php71w-mbstring php71w-mysqlnd php71w-opcache php71w-pdo php71w-xml
安装完php后重启nginx,此时不会报错
systemctl restart nginx
什么都没有显示代表nginx 配置正确
接下来修改php配置文件
vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
保存并退出
重启php-fpm
systemctl restart php-fpm
同php,加入mysql源
cd ~ #回到根目录
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm #下载源
yum localinstall mysql57-community-release-el7-8.noarch.rpm #安装源
yum install mysql-community-server #安装mysql服务
systemctl start mysqld #启动mysql服务
systemctl enable mysqld #开机自启
grep 'temporary password' /var/log/mysqld.log #可以得到默认密码
mysql -u用户名 -p #-u之后没有空格直接打用户名,会车之后会让你输入默认密码
mysql->set password for root@localhost = password('你的密码'); #修改密码
mysql->create database wordpress; #创建Wordpress使用的数据库
mysql->exit;#退出wordpress
上面nginx配置文件中有一句是配置根目录,需要把Wordpress解压内容直接放在网站根目录下就完成了。确保你的根目录是nginx用户的权限chown -R nginx:nginx 你的根目录路径 #比如chown -R nginx:nginx /usr/share/nginx/html
这个根目录就是nginx配置文件中的。
## https部署
有些站长想要使用https协议,使用https需要先到域名服务商出申请一个免费的ssl证书,之后将证书挂载到你的域名上,然后下载nginx 版本证书
。下载完成后上传到服务器,如果是按照我的教程来建议在 /etc/nginx/下新建一个 cert文件夹mkdir /etc/nginx/cert
建好文件夹后将下载的两个文件传到cert文件夹中。下一步修改配置文件,修改文件之前最好备份一份 cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
之后修改配置文件vim /etc/nignx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
#gzip compress css,js
gzip on; #这些带gzip的是用来压缩js,css
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
client_max_body_size 256M;
client_body_buffer_size 10M;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.gaojfblog.xyz; #你的域名
root /usr/share/nginx;
#Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
# enforce https
return 301 https://$server_name$request_uri; #让你的http访问重定向到443端口
}
#这些是原来自带的不用管他
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# location ~ \.php$ {
# root /usr/share/nginx;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /usr/share/nginx$fastcgi_script_name;
# include fastcgi_params;
# }
#
}
server {
listen 443;
server_name www.gaojfblog.xyz; #你的域名
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/文件名.pem; #这部分就是你刚刚上传的密钥路径下面是另一个文件 比如cert/abc.pem
ssl_certificate_key cert/文件名.key; #上传的另一个密钥文件
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html; #之前在80端口配置的根目录已经没用了这里重新配置,下面也是见到root修改成你想要的根目录就好,其他的照抄
index index.html index.htm index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location /wp-content/uploads/dlm_uploads {
deny all;
return 403;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
#download file protect
location ~ ^/wp-content/uploads/edd/(.*?)\.zip$ { rewrite / permanent; }
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}location ~* \.(pdf)$ {
expires 30d;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
记住你的主机在服务商那里443端口已经开放才可以使用。之后刷新网站就可以看见网站有小锁头了。