CentOS 7的web环境等

原则:尽量使用yum repo安装

CentOS 官方对第三方repo的说明  https://wiki.centos.org/zh/AdditionalResources/Repositories

nginx

使用nginx 官方提供的repo,配置文件 /etc/yum.repos.d/nginx.repo 文件内容如下

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

更多参看 http://nginx.org/en/linux_packages.html

php(fastcgi版)

因为要配合nginx,所以要选择fastcgi版的php-fpm, 这里选择remi源,这也是centos 官方推荐的php源之一。如下命令配置repo,将会配置好几个repo文件,但只有remi-safe.repo 是默认激活的,其他repo中的包有可能与系统文件冲突,所以在需要使用时在临时激活。

rpm -ivh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

如下命令安装php-fpm

yum --enablerepo=remi-php73 install php-fpm

remi维护了多个php版本,选用目前最新的7.3,通过--enablerepo临时激活该源。注意包名带 "-fpm" 否则是apache的模块版。注意一下安装进程,没有自动安装httpd。

按需要安装其他php扩展,下面是常用扩展的参考

yum --enablerepo=remi-php73 install php-gd php-gmp php-igbinary \
  php-mbstring php-mcrypt php-memcached php-mysqli php-odbc \
  php-pdo php-xml php-zip php-zlib php-bcmath php-imagick

配置php-fpm。主要是文件  /etc/php-fpm.d/www.conf ,默认fastcgi进程数是动态管理的,根据机器内存、负载等控制进程数,下面是微小站点的配置,其中user/group 不改默认apache也行。(或许默认的apache与后面提到的session文件目录默认权限是相符的,待确定 TODO )

user = nobody
group = nobody
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 5

修改php默认的session文件存储目录权限,按前面php-fpm用户设置为

chown nobody:nobody /var/lib/php/session

在nginx启用php

nginx主配置文件为 /etc/nginx/nginx.conf,站点配置文件在 /etc/nginx/conf.d/*.conf ,默认参看主配置文件结尾处 include /etc/nginx/conf.d/*.conf 一行。

推荐习惯:把web站点文件放在 /var/www/html/ 目录下,每个站点一个子目录,下面以默认站点文件的配置为例,其他站点参考即可。更多参考nignx官方文档,尤其是其中core模块 ngx_http_core_module

为默认站点 default.conf 创建目录 mkdir -p /var/www/html/default ,备份一下自带的的站点文件 cp /etc/nginx/conf.d/default.conf{,__original} ,参考下面示例修改 /etc/nginx/conf.d/default.conf 文件。有更多站点,用default.conf 作模板修改。选项说明:

  • 其中 listen 80 default; 一行表示这是默认站点,其他站点就不能加了。
  • 原来在location / {...} 中的root 指令; 推荐直接放到server{...}节点中,目的是,在php 配置fastcgi_param中使用$document_root 变量,而不是像默认配置文件里的再写一遍绝对路径,其中也可以省略掉root指令了。
  • try_files $uri $uri/ /index.php; 一行是wordpress的伪静态rewrite规则;
  • rewrite ^/feed /license.txt; 是把url重写到指定文件上
  • 如果配置ssl/https,可以使用Let’s Encrypt免费证书,步骤及设置参数可参看这里
server {
    listen 80 default;
    server_name blog.path8.net;
    #server_name blog.path8.net www.path8.net; #多域名
    root /var/www/html/default;
    #autoindex on;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location /robots.txt {
        log_not_found off;
        access_log off;
    }

    location / {
    #    root   /usr/share/nginx/html;
        index  index.html index.htm;
        #try_files $uri $uri/ /index.php;
        #rewrite ^/feed /license.txt;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

修改完成后,nginx -t 测试配置文件,无误后启动(start)nginx及php-fpm,并设置自动启动(enable)

nginx -t
systemctl start nginx
systemctl status nginx
systemctl enable nginx
systemctl start php-fpm
systemctl status php-fpm
systemctl enable php-fpm

如果静态文件正常,php请求报错 File not found ,并且nginx error日志中有如下错误 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream 可去检查web文件权限与php-fpm用户是否匹配,还有是不是开着SELinux且没配置好规则。

数据库MySQL/MariaDB安装

遵循CentOS7的改变,使用Mariadb替换MySQL,使用上差别不大,基本上完全沿用MySQL,除了改个名字。yum安装服务端及客户端

yum install mariadb mariadb-server

先不急着启动服务,修改一下配置文件,参考下面,放到[mysqld]节点后,大概第10行。这是适合微小型服务器的配置参数,可以按实际调整。的话添加下面几行。

#add by fengyqf start 161105
skip-name-resolve

innodb_log_buffer_size=32M
innodb_buffer_pool_size=64M
innodb_log_file_size=16M
innodb_additional_mem_pool_size=16M

server-id=301
log-bin=mysql-bin
log-error=mysql-bin.err
expire_logs_days=30
open_files_limit=10240
#add by fengyqf end

其中server-id起的4行,是配置复制功能用的,不要MySQL复制功能的话不要加,它们会生成一系列二进制文件,多占用磁盘。复制是自动同步把数据到其他服务上,还要其他配置才行,只这几行不够的。保存好启动服务。

systemctl start mariadb
systemctl status mariadb
systemctl enable mariad

如果在修改配置前启动过服务,启动会失败,删除日志文件再启动即可 rm /var/lib/mysql/ib_logfile*

new section

CentOS 7的web环境等》有一个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据