Centos7安装部署Nginx、Php7.2、MySQL5.7、WordPress

准备环境


WordPress基于PHP开发的,本文采用Centos7.3、Nginx、MySQL5.7、PHP7.2部署。

设置阿里云镜像


  1. 备份原来的yum源

    1
    sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  2. 设置阿里云的yum源

    1
    sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  3. 添加epel源

    1
    sudo wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
  4. 清理缓存并生成新的缓存

    1
    2
    sudo yum clean all  
    sudo yum makecache

安装Nginx


  1. 安装nginx源
    执行以下命令,安装该rpm后,在/etc/yum.repos.d/目录中看到一个名为nginx.repo 的文件

    1
    rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  2. 安装nginx

    1
    yum install -y nginx
  3. nginx的默认目录

    nginx配置路径:/etc/nginx/
    pid目录:/var/run/nginx.pid
    错误日志:/var/log/nginx/error
    访问日志:/var/log/nginx/access.log
    默认站点目录:/usr/share/nginx/html

只需知道nginx配置路径即可,一会儿仅需修改/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf

  • 修改配置文件
    修改/etc/nginx/conf.d/default.conf中下面两段内容:
    1
    vi /etc/nginx/conf.d/default.conf

更改前:

1
2
3
4
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

更改后:

1
2
3
4
5
 root   /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}

更改前:

1
2
3
4
5
6
7
#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;
#}

更改后:

1
2
3
4
5
6
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}

修改

1
2
```
vi /etc/php-fpm.d/www.conf

user = apache改为user = nginx,将group = apache改为group = nginx

  1. nginx的常用命令
  • 启动

    1
    2
    3
    /usr/sbin/nginx –s start
    //或者
    systemctl start nginx.service
  • 重启

    1
    2
    3
    /usr/sbin/nginx –s reload
    //或者
    systemctl restart nginx.service
  • 关闭

    1
    2
    3
    /usr/sbin/nginx –s stop
    //或者
    systemctl stop nginx.service
  • 查看进程

    1
    ps -ef |grep nginx

安装MySQL

  1. 安装MySQL5.7
  • 下载并安装MySQL官方的 Yum Repository

    1
    wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
  • yum安装

    1
    yum -y install mysql57-community-release-el7-10.noarch.rpm
  • 安装MySQL Sever

    1
    yum -y install mysql-community-server
  1. MySQL数据库设置
  • 首先启动MySQL

    1
    systemctl start  mysqld.service
  • 查看MySQL运行状态
    出现Active: active(runing)则表示启动成功

    1
    systemctl status mysqld.service
  • 此时已运行成功并进入MySQL,执行以下命令找到密码

    1
    grep "password" /var/log/mysqld.log
  • 进入数据库

    1
    mysql -uroot -p
  • 在数据库中修改密码,下面的new password填写新密码(密码设置格式为大小写、字符等)

    1
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
  • 由于安装了yum repository,以后每次yum操作都会自动更新,需要把这个卸载掉

    1
    yum -y remove mysql57-community-release-el7-10.noarch

安装PHP


  1. 如果之前已经安装先卸载之前的

    1
    yum -y remove php*
  2. 由于linux的yum源不存在php7.x,所以我们要更改yum源

    1
    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
  3. 安装php扩展,选择自己需要的

    1
    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
  4. 检验是否安装成功

    1
    php -V
  5. 开启服务

    1
    2
    3
    systemctl start nginx.service
    systemctl start mysqlb.service
    systemctl start php-fpm.service

安装WordPress

  1. 移除/usr/share/nginx/html内所有文件

    1
    2
    cd /usr/share/nginx/html
    rm 50x.html index.html
  2. 下载并解压wordpress安装包

    1
    2
    wget https://cn.wordpress.org/wordpress-5.0.2-zh_CN.zip
    tar -zxvf wordpress-5.0.2-zh_CN.zip

FAQ


  1. 远程连接MySQL出现1130错误,无法远程连接:error 1130: host ‘192.168.1.3’ is not allowed to connect to this MySQL
    1
    2
    mysql> use mysql
    mysql> select host, user from user;
  • 将相应用户数据表中的host字段改成’%’;

    1
    update user set host='%' where user='root';
  • 刷新保存,重新连接即可

    1
    flush privileges;