搭建wordpress

Ubuntu

1.安装nginx

apt-get install libpcre3-dev zlib1g-dev gcc make -y

wget http://nginx.org/download/nginx-1.20.1.tar.gz

tar -zxvf ./nginx-1.20.1.tar.gz

cd nginx-1.20.1

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf  --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module

make 

make install

cd /usr/local/nginx

cp nginx /usr/bin

启动

nginx

2.安装mysql

apt-get install mysql-server -y

检测是否安装成功

mysql

3.安装php

apt-get install php-fpm php-mysql

检测是否安装成功

echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

打开/etc/php/7.4/fpm/pool.d/www.conf文件可以确定生成的sock文件路径为/run/php/php7.4-fpm.sock且用户为www-data取消listen.mode的注释这是使用unix socket的必要文件修改如下

user = www-data
group = www-data
listen = /run/php/php7.4-pm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

修改nginx.conf文件内容

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.php;
        }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ /.php$ {
            fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
             include fastcgi.conf;
        }
    }

同时修改nginx.conf中的参数user保证nginx的用户需要与php-fpm中指定的用户一致

user  www-data;

重启nginx和php-fpm

systemctl restart php7.4-fpm

nginx -s reload

4.安装wordpress

1在nginx目录下下载wordpress中文版然后放到html目录下并解压当然也可以放在其他位置上只需要修改nginx.conf配置文件即可

wget https://cn.wordpress.org/latest-zh_CN.tar.gz 

tar -zxvf latest-zh_CN.tar.gz  wordpress

mv wordpress /usr/local/nginx/html/wordpress

2修改nginx.conf文件中server的root路径

server {
        listen       80;
        server_name  localhost;
        root   html/wordpress;
        location / {
            index  index.php;
        }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ /.php$ {
            fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
             include fastcgi.conf;
        }
    }

3登入mysql

mysql

4创建一个叫blog的数据库

create database blog;

5创建wordpressAdmin用户并授予其操作blog及其以下所有表的权限

create user wordpressAdmin identified by 'wordpressAdmin的密码';

grant all privileges on blog.* to wordpressAdmin;

注授予用户权限的操作应该在登录了root的情况下进行。

6重启mysql

systemctl restart mysql

7生成wordpress配置文件

cp /usr/local/nginx/html/wordpress/wp-config-sample.php /usr/local/nginx/html/wordpress/wp-config.php

8修改wp-config.php文件中的以下内容

/** The name of the database for WordPress */
define( 'DB_NAME', 'blog' );

/** MySQL database username */
define( 'DB_USER', 'wordpressAdmin' );

/** MySQL database password */
define( 'DB_PASSWORD', 'wordpressAdmin的密码' );

9重启nginx:

nginx -s reload

10开启wordpress的SSLhttps://www.php.cn/cms/wordpress/425534.html

在wordpress的wp-config.php文件中添加以下内容

define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

11开启nginx中SSL模块

 server {
        listen 80 default backlog=2048;
        listen 443 ssl;
        root html/wordpress;
        server_name  localhost;

        ssl_certificate      /usr/local/nginx/ssl/nginx.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/nginx.key;

        # ssl_session_cache    shared:SSL:1m;
        # ssl_session_timeout  5m;

        # ssl_ciphers  HIGH:!aNULL:!MD5;
        # ssl_prefer_server_ciphers  on;

        location / {
            index  index.php index.html index.htm;
        }
}
server {
        listen       80;
        server_name  localhost;
        root html/wordpress;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            index  index.php index.html index.htm;
        }
  	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
  location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_pass    unix:/run/php-fpm/php-fpm.sock;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

error

“/usr/local/nginx/html/wordpress/index.php” is forbidden (13: Permission denied), client: 127.0.0.1, server: localhost, request: “GET / HTTP/1.1”, host: “localhost”

这种情况请给用户添加wordpress的可执行权限

chmod 755 -R /usr/local/nginx/html/wordpress/

connect() to unix:/run/php/php7.4-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: “GET /info.php HTTP/1.1”, upstream: “fastcgi://unix:/run/php/php7.4-fpm.sock:”, host: “localhost”

通过/etc/php/7.4/fpm/pool.d/www.conf文件内容可知fpm用户为www-data,只需要修改nginx.conf中的用户为www-data以保证用户一致。

centos7

1. 安装nginx

wget http://nginx.org/download/nginx-1.20.1.tar.gz

tar -zxvf ./nginx-1.20.1.tar.gz

yum install -y pcre pcre-devel 

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel 

cd nginx-1.20.1

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf  --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module

make

make install

​ 为了能够在任意目录使用nginx命令需要将nginx可执行文件复制一份到/usr/bin目录下。

cd /usr/local/nginx

cp nginx /usr/bin

​ 启动nginx后如果无法访问nginx安装好的默认网页则修改防火墙配置

vi /etc/sysconfig/iptables

​ 添加一行内容

-A INPUT -p tcp -m tcp --dport 端口号 -j ACCEPT

​ 这个端口号是nginx.conf文件中你要提供服务的那个server的监听端口号当然如果不想使用这个端口可以修改端口号。

​ 保存退出之后重启

systemctl restart iptables

​ 如果是本机浏览器地址栏输入http://localhost/如果是服务器浏览器地址栏输入http://服务器公网ip/如果可以看到welcome to nginx页面则代表安装成功。

2.安装mariadb

参照腾讯云。

云服务器 手动搭建 LNMP 环境CentOS 7 - 最佳实践 - 文档中心 - 腾讯云 (tencent.com)

1查看mariadb版本信息如果必要移除旧版本不移除旧版本直接从忽略mariadb的安装

rpm -qa | grep -i mariadb

yum -y remove mariadb

2新建MariaDB.repo文件提供yum库

vi /etc/yum.repos.d/MariaDB.repo

3打开MariaDB.repo文件输入以下内容

# MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = https://mirrors.cloud.tencent.com/mariadb/yum/10.4/centos7-amd64
gpgkey=https://mirrors.cloud.tencent.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

4安装

yum -y install MariaDB-client MariaDB-server

5开始运行并启动开机自启

systemctl start mariadb

systemctl enable mariadb

6测试是否安装成功如若失败看步骤七

mysql

7如果出现以下错误

​ ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: NO)

​ 使用root密码登录

mysql -u root -p

​ 之后输入密码即可。

如果不行这说明密码被修改过你的mariadb并不是新装好的。

3.安装php

安装参照云服务器 手动搭建 LNMP 环境CentOS 7 - 最佳实践 - 文档中心 - 腾讯云 (tencent.com)

1查看版本如果未安装直接跳到步骤3

php -v

2可手动卸载较低版本的php

yum -y remove mod_php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-mysqlnd php70w-fpm.x86_64

3更新yum中php的软件源

rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm

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

4安装

yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64

5启动并开启php-fpm开机自启动

systemctl start php-fpm

systemctl enable php-fpm

6验证php-fpm环境配置

​ 生成一个php文件用于测试

echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

​ 修改nginx中的内容

​ 添加一个index.php

location / {
            root   html;
            index  index.php index.html index.htm;
}

​ 开启fastcgi:

#取消注释前
#location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

# 取消注释后
location ~ \.php$ {
	include fastcgi.conf;
    fastcgi_pass    unix:/run/php-fpm/php-fpm.sock;
}

​ 并修改/etc/php-fpm.d/www.conf文件内容

# 修改前
;listen = 127.0.0.1:9000
# 修改后的
listen = /run/php-fpm/php-fpm.sock

​ 这个时候依旧不能访问需要统一nginx和php-fpm的user

​ 为了安全性可以选择为nginx服务专门添加一个不能登录也没有家目录的nginx用户这个用户用于nginx借助fastcgi和php-fpm进行交流:

useradd -M -s /sbin/nologin nginx

​ www.conf文件中

user = nginx
;listen = 127.0.0.1:9000  用于TCP的需要注释
listen = /run/php-fpm/php-fpm.sock ;用于unix socket 应取消注释
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

​ nginx.conf文件中

user nginx;

​ 配置完后记得重启php-fpm和nginx:

systemctl restart php-fpm

nginx -s reload

7一开始fastcgi是默认使用TCP的但是我尝试默认的却一直出错还找不出解决办法无奈之下只能选择unix socket了。

4.安装wordpress

1下载wordpress安装包并解压至nginx下的html目录中

wget https://cn.wordpress.org/latest-zh_CN.tar.gz 

tar -zxvf latest-zh_CN.tar.gz  wordpress

mv wordpress /usr/local/nginx/html/wordpress

2修改root路径

user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
 
    keepalive_timeout  65;
    server {
        listen       9999;
        server_name  localhost;
        root html/wordpress;
        location / {
                index index.php;
        }
        error_page  404              /404.html;
		error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_pass    unix:/run/php-fpm/php-fpm.sock;
        }
    }
}

3root登录数据库

mysql -u root -p

​ 输入密码后创建blog数据库并赋予nginx对该数据库的所有权限

create database blog;

create user wordpressAdmin identified by '数据库用户wordpressAdmin的密码';

grant all privileges on blog.* to 'wordpressAdmin'@'localhost' identified by '数据库用户wordpressAdmin的密码';

flush privileges;

\q

4生成wordpress的配置文件wp-config.php

cp /usr/local/nginx/html/wordpress/wp-config-sample.php /usr/local/nginx/html/wordpress/wp-config.php

5修改wp-config.php文件中的以下内容

/** The name of the database for WordPress */
define( 'DB_NAME', 'blog' );

/** MySQL database username */
define( 'DB_USER', 'wordpressAdmin' );

/** MySQL database password */
define( 'DB_PASSWORD', 'wordpressAdmin的密码' );


6重启nginx

nginx -s reload

搭建成功。

error

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

​ 这是没开启mysqld服务。开启服务

systemctl start mysqld

拓展

1.重置mysql root密码

​ 这个时候如果不知道mysql root的密码的话在root下执行以下操作

systemctl stop mysqld

mysqld_safe --user=mysql --skip-grant-tables --skip-networking 
mysql -u root mysql

UPDATE user SET Password=PASSWORD('root新密码') where USER='root';

FLUSH PRIVILEGES;

quit;
systemctl start mysqld
chown -R nginx /usr/local/nginx/html/wordpress

chmod -R 700 /usr/local/nginx/html/wordpress

nginx -s reload

使用systemctl管理mysqld服务被卡住了

​ 这是服务被阻塞的表现但是实际上并没影响使用所以也就没多管。

recv() failed (104: Connection reset by peer) while reading response header from upstream,

​ 可以看出nginx 与php-fpm 间的通信出现了问题再查看access.log发现"GET /index.php HTTP/1.1" 502 494

​ 查询了网上很多资料但发现每一个对应我这种情况使用的fastcgi 通过TCP通讯但是并没通讯成功说是一般是读取资源的问题没有执行完毕而导致PHP-CGI进程终止但服务器就我一个人使用应该不存在资源不够用的情况

​ Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行但是由于某种原因一般是读取资源的问题没有执行完毕而导致PHP-CGI进程终止。

PRIVILEGES;

quit;


```shell
systemctl start mysqld
chown -R nginx /usr/local/nginx/html/wordpress

chmod -R 700 /usr/local/nginx/html/wordpress

nginx -s reload

使用systemctl管理mysqld服务被卡住了

​ 这是服务被阻塞的表现但是实际上并没影响使用所以也就没多管。

recv() failed (104: Connection reset by peer) while reading response header from upstream,

​ 可以看出nginx 与php-fpm 间的通信出现了问题再查看access.log发现"GET /index.php HTTP/1.1" 502 494

​ 查询了网上很多资料但发现每一个对应我这种情况使用的fastcgi 通过TCP通讯但是并没通讯成功说是一般是读取资源的问题没有执行完毕而导致PHP-CGI进程终止但服务器就我一个人使用应该不存在资源不够用的情况

​ Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行但是由于某种原因一般是读取资源的问题没有执行完毕而导致PHP-CGI进程终止。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6