CentOS 7 安装php7 + MariaDB + Nginx + Wordpress

一、安装Fedora第三方源

  1. 安装Fedora第三方源

    yum -y update
    yum -y install epel-release
    
  2. 修改epel为默认不启用

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

    将[epel]部分的enabled=1修改成enabled=0

二、安装最新版MariaDB数据库:

参考:https://downloads.mariadb.org/mariadb/repositories/#mirror=yamagata-university&distro=CentOS&distro_release=centos7-amd64–centos7&version=10.2

  1. 编辑yum源文件

    # vim /etc/yum.repos.d/MariaDB.repo
    

    内容:

    # MariaDB 10.2 CentOS repository list - created 2017-06-24 17:08 UTC
    # http://downloads.mariadb.org/mariadb/repositories/
    [mariadb]
    name = MariaDB
    baseurl = http://yum.mariadb.org/10.2/centos7-amd64
    gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
    gpgcheck=1
    
  2. 安装mariadb服务器

    # yum -y update 
    # yum install -y mariadb-server mariadb
    
  3. 修改設定

    亚洲语言的用户,建议将默认服务器内码修改成utf8。另外根据实际需要修改其它的配置。 vi /etc/my.cnf.d/server.cnf, 增加以下配置。

    [mysqld]
    character-set-server = utf8
    
    # 根据主机性能及实际需要,设置最大连接数
    max_connections=350
    
    # 如果需要外部主机访问,可以该项。(需要防火墙配合)
    bind-address=0.0.0.0    
    
  4. 开启mariadb服务:

    # systemctl start mariadb
    # systemctl enable mariadb
    
  5. 查看状态:

    # systemctl status mariadb
    # systemctl is-enabled mariadb.service
    enabled
    
  6. 初始化数据库安全设置:

    # mysql_secure_installation
    

    推荐选项:

    Set root password? [Y/n] Y
    Remove anonymous users? [Y/n] Y
    Disallow root login remotely? [Y/n] Y
    Remove test database and access to it? [Y/n] Y
    Reload privilege tables now? [Y/n] Y
    

三、安装PHP7

  1. 导入remi源(包含最新版的PHP7)

    rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    

    注意:remi源的默认设置就是enable=0,所以使用时必须增加--enablerepo参数调用。

  2. yum安装PHP7及常用的模块

    yum install --enablerepo=epel,remi-php70 php php-mbstring php-pear php-fpm php-mcrypt php-mysql php-gd php-xml 
    
    pear install DB
    
  3. 确认版本

    php -v
    

四、安装配置Nginx

  1. yum安装nginx

    # yum --enablerepo=epel install nginx
    
  2. 设置nginx自动启动

    # systemctl enable nginx
    

五、配置php-fpm服务

  1. 修改php-fpm的默认设置

    上一步安装的php-fpm,默认设置里运行用户都是apache,需要先修改成nginx

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

    需要修改的内容:

    user = nginx
    group = nginx
    listen.owner = nginx
    listen.group = nginx
    
    ;如果不需要外部主机访问该php-fpm的话,推荐改为监听unix socket.
    listen = /var/run/php-fpm/php-fpm.sock
    
  2. 启动php-fpm服务

    # systemctl start php-fpm
    # systemctl enable php-fpm
    

六、安装配置WordPress站点

WordPress的官网下载地址:https://wordpress.org/download/

  1. 确定站点所在的上级目录,如果没有就创建,例如:/opt/www

    # mkdir -p /opt/www
    
  2. 下载最新版的WordPress, 例如, 4.8版本,然后解压

    # cd /opt/www
    # wget https://wordpress.org/latest.zip
    # unzip latest.zip
    

    解压后的目录名是wordpress, 可以根据自己的需要修改成需要的名称

    # mv wordpress mysite
    

    修改该目录的拥有者为nginx用户(重要!)

    # chown -R nginx:nginx /opt/www/mysite
    
  3. 创建所需的MySQL数据库

    例如创建数据库my_site_db,用户名为my_db_user

    连接数据库:

    # mysql -p    
    

    在mysql终端下,执行创建数据库及设置用户权限及密码的SQL语句

    mysql> CREATE DATABASE my_site_db DEFAULT CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI;
    mysql> GRANT ALL PRIVILEGES ON my_site_db.* TO my_db_user@'%' IDENTIFIED BY 'my_password';
    mysql> GRANT ALL PRIVILEGES ON my_site_db.* TO my_db_user@'localhost' IDENTIFIED BY 'my_password';
    
    
  4. 设置nginx站点配置

    编辑配置文件

    # vi /etc/nginx/conf.d/mysite.conf
    

    配置样例一:http站点

    server {
        listen 80;
        server_name www.mysite.com;
        root /opt/www/mysite;
        index index.php;
    
        charset utf-8;
    
        try_files $uri $uri/ /index.php?q=$uri&$args;
    
        location ~* /wp-config.php {
            deny all;
        }
    
        location ~* \.(eot|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
        }
    
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
    

    配置样例二:https站点

    参照我整理的使用acme.sh脚本及DNS API方式申请及更新let’s encrypt证书一文。

  5. 启动nginx

    测试配置文件是否正常:

    # nginx -t
    

    重启动nginx:

    # systemctl start nginx
    
  6. 修改php-fpm的unix目录的用户及组

    chown nginx:nginx /var/run/php-fpm/ -R
    chown nginx:nginx /var/lib/php/ -R
    

七、防火墙设置

  1. 如果没有开启firewalld,需要开启。

    # systemctl enable firewalld
    # systemctl start firewalld
    # systemctl status firewalld
    
  2. 允许http及https服务

    firewall-cmd --permanent --add-service=http 
    firewall-cmd --permanent --add-service=https
    firewall-cmd --reload
    firewall-cmd --list-services
    
Share Comments
comments powered by Disqus