• home > OS > Linux > Devops >

    服务器升级至centos8 网站配置-php与mysql从5.6升级到php7与msyql8

    Author:zhoulujun Date:

    本文是本人播客从centos6 5 php5 6 mysql5 6 nginx-1 1 13 升级到centos8 mysql8 php7 nginx1 17 7,从纯净版centos 启动开始,到网站运行,再到优化,经过不少坑。centos8 国内用的人应该不多,迁移入坑的话,不烦看下本文。

    linux centos8 安装php7 nginx1.4 mysql8 ,运行php网站,各个模块从零开始配置

    CentOS7与centOS8的抉择

    目前国内各大云服务器的默认centos 系统版本还是7,目前没有centos配套支持,国外云服务器厂商默认就是8,如vultr

    centos8的性能提升,官网介绍的更清楚

    安装centos8 min版本,在我的系统上查看,发现nginx默认是1.14

    开发语言版本

    • Python 3.6

    • PHP 7.2

    • Ruby 2.5

    • Node.js 10

    • java::OpenJDK 11


    数据库版本

    • MySQL 8.0

    • MariaDB 10.3

    • PostgreSQL 10 and PostgreSQL 9.6

    • Redis 5.0

    而我的服务器msyql是msyql5.6 php是php5.6 ,我是选择重新来过一遍。

    centos8 从零开始安装nginx mysql php 系统默认版本搭建php网站

    centos8 安装nginx

    • 安装nginx

      yum install nginx php 

    • 启动nginx

      systemctl start nginx.service

    • 查看nginx状态

      systemctl status nginx

    • 设置nginx开机启动

      systemctl enable nginx.service

    centos8 安装php

    • 安装php

    • yum install  php

    • 安装php拓展库 

    • yum install  php-json php-xml  php-mysqlnd php-mbstring  php-common  php-gd

    • 启动php

    • systemctl start php-fpm

    • 查看php版本

    • php -V

    • 设置php开机启动

    • systemctl enable php-fpm

    centos8安装mysql

    • 查看是否安装MySQL

      rpm -qa | grep mysql

    • 下载安装包文件

      wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

    • 安装mysql-community-release-el7-5.noarch.rpm包

      rpm -ivh mysql-community-release-el7-5.noarch.rpm

    • 查看可用安装包

      yum repolist all | grep mysql

    • 安装mysql

      yum install mysql-server

    • 检查mysql是否安装成功。

      rpm -qa | grep mysql

    • 启动 mysql 服务 

      systemctl start mysqld.service

    当然,现在最新的mysql 源

    wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm

    具体的,推荐阅读《centos按照mysql教程

    网站nginx selinux 文件权限配置

    网站nginx配置

    nginx基本配置,这里就不在多说了,查看《Nginx葵花宝典—草根站长配置Nginx运维百科全书

    像我这种小水管网站,需要注意的就是,nginx 限流

    大致配置如下

        # 1M能存储16384个状态,rete的值必须为整数,
        # 如果限制两秒钟一个请求,可以设置成30r/m ,其中$binary_remote_addr有时需要根据自己已有的log_format变量配置进行替换
        limit_conn_zone $binary_remote_addr zone=perip:1m;
        limit_conn_zone $server_name zone=perserver:1m;
        #limit_req   zone=perip burst=10;
         # 限制客户端并发连接数量为20, allow only one connection per an IP address at a time(每次). ;
        #是限制每个IP只能发起20连接 (addr 要跟 limit_conn_zone 的变量对应)
        # 表明以ip为key,来限制每个ip访问lmit.html文件时候,最多只能有一个在线,否则其余的都要返回不可用。
        limit_conn perip 14;
        limit_conn perserver 10;
        limit_req_zone $binary_remote_addr zone=per_ip:1m rate=400r/s;
        limit_req_zone $server_name zone=per_server:10m rate=600r/s;
        limit_req zone=per_ip burst=300 nodelay;
        limit_req zone=per_server burst=500;

    具体,推荐阅读《Nginx下limit_req模块burst参数超详细解析

    nginx配置目录结构如下

    微信截图_20200120180035.png


    这里把本站配置贴一下

    nginx基本配置

    nginx配置文件为HOCON,intellij编辑的,配置查看:《HOCON:nginx配置文件后缀conf是什么格式类型文件夹?intellij如何编辑》,其他编辑器应该也有相应插件。有个插件认识代码,编辑器起来应该舒服些。

    # 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/doc/nginx/README.dynamic.
    include /etc/nginx/modules/*.conf;
    
    events {
        # essential for linux, optmized to serve many clients with each thread
          # Linux 关键配置,允许单个线程处理多个客户端请求。
          use epoll;
          # Determines how many clients will be served by each worker process.
          # (Max clients = worker_connections * worker_processes)
          # "Max clients" is also limited by the number of socket connections available on the system (~64k)
          # 配置单个 Nginx 单个进程可服务的客户端数量,(最大值客户端数 = 单进程连接数 * 进程数 )
          # 最大客户端数同时也受操作系统 socket 连接数的影响(最大 64K )
          worker_connections 51200;
    
          #用来配置nginx服务器是否可能多地接收客户端的连接请求,默认值为off
          multi_accept on;
    }
    # http config
    include /etc/nginx/http/default.conf;
    #include /etc/nginx/http/http_web.conf;

    nginx http配置

    http {
    ################################ logs  #######################
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        # Buffer log writes to speed up IO, or disable them altogether
        # 将日志写入高速 IO 存储设备,或者直接关闭日志。
        # access_log /var/log/nginx/access.log main buffer=16k;
        access_log off;
        # only log critical errors 只记录 critical 级别的错误日志
        error_log /var/log/nginx/error.log crit;
    
    ################################  file  #######################
        # types
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        charset UTF-8;
        # 只允许get post 请求
        add_header 'Access-Control-Allow-Methods' 'GET, POST';
        #隐藏掉nginx的版本号
        server_tokens off;
    ################################ 开启gzip压缩 相关配置 #######################
        gzip on;
        gzip_disable   "MSIE [1-6]\.";
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 4;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
    
        #  请求设置优化配置 #######################
        tcp_nodelay         on;
        # sendfile() 不但能减少切换次数而且还能减少拷贝次数。
        sendfile            on;
        # 使缓冲区中的数据立即发送出去
        tcp_nopush          on;
        # 指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒
        # 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接
        keepalive_timeout   65;
        types_hash_max_size 2048;
        client_max_body_size 2m;
        # Number of requests a client can make over the keep-alive connection. This is set high for testing.
        # 单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。
        keepalive_requests  10000;
        # Timeout for keep-alive connections. Server will close connections after this time.
        # 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接。
        # 客户端向服务端发送一个完整的 request header 的超时时间。如果客户端在指定时间内没有发送一个完整的 request header,Nginx 返回 HTTP 408(Request Timed Out
        client_header_timeout 40s;
        # send the client a "request timed out" if the body is not loaded by this time. Default 60.
        # 指定客户端与服务端建立连接后发送 request body 的超时时间。如果客户端在指定时间内没有发送任何内容,Nginx 返回 HTTP 408(Request Timed Out)
        client_body_timeout 40s;
        reset_timedout_connection on;
        # If the client stops reading data, free up the stale client connection after this much time. Default 60.
        # 客户端数据读超时配置,客户端停止读取数据,超时时间后断开相应连接,默认是 60 秒。 服务端向客户端传输数据的超时时间
        send_timeout 30;
        server_names_hash_bucket_size 128;
        #客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
    
    ################################ 限速配置 ################################
        limit_conn_log_level error;
        limit_conn_status 503;
        #limit_conn_zone $binary_remote_addr zone=one:1m;
        #limit_conn_zone $server_name zone=perserver:1m;
        # 定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,
        # 以$binary_remote_addr 为key,限制平均每秒的请求为20个,
        # 1M能存储16384个状态,rete的值必须为整数,
        # 如果限制两秒钟一个请求,可以设置成30r/m ,其中$binary_remote_addr有时需要根据自己已有的log_format变量配置进行替换
        limit_conn_zone $binary_remote_addr zone=perip:1m;
        limit_conn_zone $server_name zone=perserver:1m;
    
        #limit_req   zone=perip burst=10;
         # 限制客户端并发连接数量为20, allow only one connection per an IP address at a time(每次). ;
        #是限制每个IP只能发起20连接 (addr 要跟 limit_conn_zone 的变量对应)
        # 表明以ip为key,来限制每个ip访问lmit.html文件时候,最多只能有一个在线,否则其余的都要返回不可用。
        limit_conn perip 14;
        limit_conn perserver 10;
        limit_req_zone $binary_remote_addr zone=per_ip:1m rate=400r/s;
        limit_req_zone $server_name zone=per_server:10m rate=600r/s;
        limit_req zone=per_ip burst=300 nodelay;
        limit_req zone=per_server burst=500;
    ################################ web server #######################
        include  /etc/nginx/http/http_web.conf;
    
    }

    nginx空域名设置,禁止ip访问

    防止别把域名解析到我们的ip服务器,造成我们的ip被墙

    # 关闭nginx空主机头 防止nginx空主机头及恶意域名指向
    server {
        listen *:80 default;
        server_name _;
        #index index.html index.php index.htm;
        #root  /data/wwwroot/zhoulujun;
        #include /etc/nginx/conf.d/php.conf;
        # rewrite ^(.*) //zhoulujun.cn permanent;
        return 301 https://www.zhoulujun.cn$request_uri;
    }

    nginx php支持配置

    ################################  php 相关配置 #######################
        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # include /etc/nginx/conf.d/php-fpm.conf
        upstream php-fpm {
            server unix:/run/php-fpm/www.sock;
        }
    ################################  php fastcgi 相关配置 #######################
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
    
    ################################ host ################################
        include  /etc/nginx/site/default.conf;

    linux服务器 web目录权限设置

    给需要读取的文件,设置644,给需要指向的文件,如php文章,设置755,给需要读写的文件夹,如html uploadfile 文件夹设置777

    具体权限设置,可以参看我之前写的文章《理清用户组概念及文件权限—搞懂网站权限设置

    • 设置全部文件为为 644

      chmod -R  644 /data/wwwroot

    • 设置文件目录为755

      find /path -type d -exec chmod 755 {} \;  //设置目录权限为755

    • 设置php类型文件为 755

      find . -name "*.php" |xargs chmod 755 {} \;

    设置了权限,由于centos8 默认开启selinux ,网站还是不能访问。一般报403错误。

    linux文件批量处理,可以参看《exec与xargs范例详解:文件目录批量操作命令

    centos8 网站服务器 selinux设置

    网上搜索,一般都是说关闭selinux,但是,个人不建议关闭

    设置selinux给web目录读权限

     chcon -v -R --type=httpd_sys_content_t /data/www/

    设置selinux给web目录写权限

    chcon -R -t httpd_sys_rw_content_t /data/www/

    具体参看《centos8 nginx server root指向自定义目录如(/data/www),访问报403 404,所有文件用户组为root 权限为755

    设置完了,网站就可以运行了


    linux网站基本安全设置

    linux添加用户,修改用户密码,修改用户权限,设置root操作

    • 添加一个名为 andyZhou的用户

      useradd andyZhou

    • 修改密码

      passwd chenjiafa

    • 给用户root权限

      usermod -g root chenjiafa

    网站平时用这个新增的用户登录,需要root权限,就su 切换

    具体查看《linux添加用户,修改用户密码,修改用户权限,设置root用户操作

    禁止root ssh登录 修改默认端口

    修改ssh配置文件

    修改ssh登录端口

    vim /etc/ssh/sshd_config

    找到“#Port 22”,这一行直接键入“yyp”复制该行到下一行,然后把两行的“#”号即注释去掉,修改成:

    #Port 22
    Port 10086

    大家修改端口时候最好挑10000~65535之间的端口号,10000以下容易被系统或一些特殊软件占用,或是以后新应用准备占用该端口的时候,却被你先占用了,导致软件无法运行

    禁止root ssh登录

    PermitRootLogin no

    增加ssh 普通登录用户

    AllowUsers andyzhou

    重启ssh服务

    systemctl restart sshd

    linux防火墙端口设置

    • linux 防火墙关闭某个端口

      firewall-cmd --permanent --zone=public --remove-port=8080/tcp

    • linux 防火墙打开某个端口

      firewall-cmd --permanent --add-port=10086/tcp

    • 重新加载防火墙策略:

      firewall-cmd --reload

    • 查看防火墙端口开放情况

       firewall-cmd --list-ports

    linux虚拟内存设置

    我的个人博客是1g小内存,mysql跑起来,经常崩溃。

    free -m  查看内存,根据情况设置内存,一般虚拟机内存为物理内存的2倍。那么设置2g虚拟内存,命令如下

    dd if=/dev/zero of=/opt/swap bs=1024 count=2048000
    chmod 600 /opt/swap
    mkswap /opt/swap
    swapon /opt/swap

    mysql创建新用户并授权,禁止root登录

    下面几行sql,应该满足您的需求

    -- 创建用户,以后用这个用户登录

    CREATE USER 'userName' @ '访问限制' IDENTIFIED BY 'password';

    -- 授权用户 ,给与数据的权限

    GRANT ALL PRIVILEGES ON 数据库名称.表名称 TO 'userName'@'访问限制';

    ---修改用户密码(修改root密码)

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'newPassword';

    -- 刷新用户权限

    FLUSH PRIVILEGES;

    网站cloudflare加速

    阿里云,导出zone文件,然后clouefare 导出模板,然后复制粘贴即可

    网上教程很多,比如《如何用CDN加速你的网站 – Cloudflare免费版详细使用教程

    这里提下需要注意的几个点

    20230305231027623089326.webp

    如果打开 Under Attack Mode ,总会出现一个5m 的启动页,而且现在免费用户,不能再customer page 里面配置

    具体如何配置,如下:《cloudflare如何自定义5秒盾页面教程

    第二是,cloudflare Rocket Loader,国内被墙了,我的异步执行,插入的js 会被这个js阻塞。国内用户不管怎么样,还是关闭好。

    cloudflare Rocket Loader中国使用者最好不要使用

    参考链接:《cloudflare Rocket Loader中国使用者最好不要使用


    网站配好后,可以在本地镜像一个,传送门:《vmware14安装centos8

    先根据个人网站迁移步骤,整理这么多。以后再来整理文字




    转载本站文章《服务器升级至centos8 网站配置-php与mysql从5.6升级到php7与msyql8》,
    请注明出处:https://www.zhoulujun.cn/html/OS/Linux/LinuxDevops/8242.html