手拉手搭建 lnmp 环境及安装 wordpress

1,246 阅读6分钟

首先了解一下环境 平台:Centos7.6 Nginx: 1.12.2 Mysql: 8 (或者 Mariadb: 10.3.16-MariaDB) PHP: 7.2.19 Wordpress: 5.2.2

另外,由于 mysql8 的官方 yum 源安装速度非常慢(亲测20 多分钟至几个小时),而且一些配置也会稍微复杂一点,所以推荐安装 mariadbb10 的版本替代。

安装开发工具包

  • 可先通过 yum grouplist查看一下安装列表,这里我们只安装其中一个开发工具包Developmennt Tools
yum groupinstall 'Development Tools' -y

nginx安装与配置

  • 安装
yum isntall -y nginx
  • 一般nginx的默认配置目录是/etc/nginx/conf.d/,这里我们新建一个配置文件
vim /etc/nginx/conf.d/web.conf
  • 并在web.conf 文件中写入以下内容,注意 server_name t1.xiaoxiangti.com; 为你的域名。
#======================== WEB options ============================
server {
        listen       80;
        server_name  t1.xiaoxiangti.com;
        root         /var/wordpress;
	index        index.php index.html;
	charset	     utf-8;
#======================== Pseudo static ==========================
        location / {
	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; }
	}
#======================== PHP options ============================
	location ~ \.php {
		root    /var/wordpress;
		fastcgi_pass    127.0.0.1:9000;
		fastcgi_index   index.php;
		fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include         fastcgi_params;
	}
#======================== Error page =============================
        error_page 400 403 404 /40x.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

最后启动nginx服务,并设为开机启动

systemctl start  nginx
systemctl enable nginx

清除旧的 mysql/mariadb 版本

centos7下默认安装有mariadb数据库,但是是旧版本,在安装新版本前需要先把旧版本删除,有些系统还默认安装mysql,也必须删除,否则有可能会产生冲突。

  • 检查是否安装了mysql及相关依赖
[root@iZwz9dsrvtc901nh4f4tbyZ ~]# rpm -qa |grep mysql
mysql80-community-release-el7-3.noarch
mysql-community-common-8.0.17-1.el7.x86_64
mysql-community-client-8.0.17-1.el7.x86_64
mysql-community-server-8.0.17-1.el7.x86_64
mysql-community-libs-8.0.17-1.el7.x86_64
mysql-community-libs-compat-8.0.17-1.el7.x86_64

如上,如果有安装的话,则依次卸载

# 通过rpm -e卸载
rpm -e mysql80-community-release-el7-3.noarch

# 卸载不成功时使用此命令强制卸载

rpm -e --nodeps mysql80-community-release-el7-3.noarch

# 也可通过yum remove -y卸载
yum remove -y mysql80-community-release-el7-3.noarch

...
  • 查找分散的mysql目录及配置文件 通过find命令
[root@iZwz9dsrvtc901nh4f4tbyZ ~]# find / -iname mysql
/usr/lib64/mysql
/usr/bin/mysql
/etc/logrotate.d/mysql
/var/lib/mysql
/var/lib/mysql/mysql

通过which命令

[root@iZwz9dsrvtc901nh4f4tbyZ ~]# which mysql
/usr/bin/mysql

上面find命令后面/表示需要查找的路径,-iname参数表示忽略大小写,查找名字中包含mysql的结果。 上面查找到的目录或文件需要rm -rf依次删除

rm -rf /usr/lib64/mysql
...

mariadb同理,把上面命令中的mysql换成mariadb再执行一遍即可。

mysql8安装与配置

配置 yum 源及安装

  • 下载mysql官方的yum源仓库
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  • 安装yum源
yum localinstall mysql80-community-release-el7-3.noarch.rpm -y

# 这里也可执行这个命令
rpm -ivh mysql80-community-release-el7-3.noarch.rpm --force --nodeps

完成后,会再yum的配置目录/etc/yum.repos.d/目录下生成对应的yum源文件,例如查看一下yum源的列表:

[root@iZwz9dsrvtc901nh4f4tbyZ ~]# yum repolist
mysql80-community/x86_64
mysql-tools-community/x86_64
mysql-connectors-community/x86_64 
...
  • 更新yum源
yum clean all && yum makecache
yum upgrade -y
  • 安装mysql 做了半天准备,终于可以安装了。这里安装的是mysql的社区版。这一步会比较慢,我试了几次,最快的也要20多分钟,慢的话可能需要一两个小时..
yum install mysql-community-server -y
  • 启动mysql 安装完成后,启动mysql,并设置为开机启动
systemctl start mysqld
systemctl enable mysqld

修改配置文件

这一环节主要针对 mysql8 及以上的版本, 如果是 mysql8 以下版本则不需要,可以直接跳过到“创建 wordpress”的环节。 因为以往的mysql版本验证机制是“mysql_native_password”,而mysql8使用的身份验证机制,所以需要修改下配置文件my.cnf

  • 打开配置文件:vim /etc/my.cnf并新增以下内容:
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8
default_authentication_plugin = mysql_native_password
  • 登录 mysql8及以后的版本会生成一个默认的root用户临时登录密码,所以如果直接通过mysql登录,可能会提示没有密码:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

我们需要先通过grep "temporary password" /var/log/mysqld.log查看临时密码

[root@iZwz9dsrvtc901nh4f4tbyZ log]# grep "temporary password"  /var/log/mysqld.log
2019-08-02T07:20:30.072655Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: g5+(wMKsdq*C

可以看到临时密码为g5+(wMKsdq*C,接着登录:

mysql -uroot -p'g5+(wMKsdq*C'
  • 修改root用户的临时密码 通过临时密码登录后,需要先修改密码。mysql8对密码要求比较高,要包含字母大小写、数字和符号。
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Slevin@123456';

创建wordpress数据库及用户

create database wpdb;  # 创建wpdb数据库
create user 'slevin'@'localhost' identified with mysql_native_password by 'Slevin@123456';  # 创建slevin账号及登录密码
grant all privileges on wpdb.* to 'slevin'@'localhost';  # 赋予slevin账号权限
  • 刷新权限及退出 完成以上操作后,刷新权限即可退出mysql
flush privileges;
exit;
  • 重启 mysql
systemctl restart mysqld

mariadb10 安装及配置

配置阿里云的 mariadb yum 源

在目录下 /etc/yum.repos.d/ 创建文件MariaDB.repo ,并把以下内容添加到所建文件中:

#MariaDB 10.3 CentOS repository list - created 2018-10-16 15:18 UTC
 
#http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB

baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/

gpgkey = http://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
 
gpgcheck=1

更新 yum 源缓存

yum clean all && yum makecache
yum upgrade -y

安装 mariadb 及相关拓展

yum install -y MariaDB-server MariaDB-client

启动 mariadb 并设为开机启动

systemctl start mariadb
systemctl enable mariadb

创建 worpdress 使用的数据库及用户

输入mysql登录数据库,然后依次执行

create database wpdb;
grant all privileges on wpdb.* to 'slevin'@'localhost' identified by 'Slevin@123456';
flush privileges;
exit;

php7安装及配置

配置 yum 源

由于linux的yum源不存在php7.x,所以我们要更改yum源。而php7.x有两个源可以选择,一个是webtatic提供的,也就是php70w,那个w指得就是webtatic,另一个可以使用remi源。这里主要以webtatic进行展开讲解。

  • 首先配置 epel 源
yum install -y epel-release

# 或者配置阿里云镜像的 epel 源
cd /etc/yum.repos.d
wget https://mirrors.aliyun.com/repo/epel-7.repo
  • 再配置 wetatic 源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  • 更新 yum 源缓存
yum clean all && yum makecache
yum upgrade -y

可以通过yum repolist查看确认一下 yum 源列表 通过yum list |grep php7查看可安装的 php7 以上版本列表信息

安装

安装 php 及相应拓展。这里安装的是7.2版本,也就是php72w,对应的拓展都是以此为前缀,如果需要其他版本如 php70w、php71w, 对应拓展的前缀也需更改。

yum install -y php72w php72w-fpm php72w-mysql php72w-common php72w-cli php72w-gd

#更全的依赖
#yum install -y php72w php72w-fpm php72w-mysql php72w-common php72w-cli php72w-gd php72wp-dba php72w-devel php72w-embedded php72w-imap php72w-interbase php72w-intl php72w-ldap php72w-mbbstring php72w-mysqlnd php72w-odbc php72w-opcache php72w-pdo php72w-pdo_dblib  php72w-pear php72w-pecl-apcu php72w-pecl-imagick php72w-pecl-redis php72w-pecl-xdebug php72w-pgsql php72w-phpdbg php72w-process php72w-pspell php72w-recode php72w-snmp php72w-soap php72w-tidy php72w-xml php72w-xmlrpc

相关配置

使用vim编辑器编辑此文件

vim /etc/php-fpm.d/www.conf

在 www.conf 文件中,修改user、group字段,这里使用“nginx”用户运行PHP服务,方便之后权限规划:

user = nginx
group = nginx

启动服务并设为开机启动

systemctl start php-fpm && systemctl enable php-fpm

安装 wordpress

依次执行以下命令:

mkdir /var/wordpress && cd /var/wordpress/
wget  https://cn.wordpress.org/latest-zh_CN.zip && unzip latest-zh_CN.zip
mv wordpress/* /var/wordpress/ && cd /var
chmod 755 -R wordpress
chown nginx:nginx -R wordpress

如果以上操作都没问题的话,这个时候访问你的域名或者服务器的ip的ip地址,就会看到wordpress的安装界面了。

安装界面这里填入上面我们创建好的mysql账户即可。

参考