使用http用户服务:(例如 nginx , http, caddy, apache 之类)

user  : nginx 
group : nginx

普通用户根据项目来定: (举例用户 userName)

user  : userName
group : nginx,users

安装配置

CentOs 7.0 开始启用 stystemd 服务管理, 使用 systemctl 来替代 service 命令,之前的区别为

service 服务名 start|stop|restart|status
systectl start|stop|restart|status  服务名

关于源

Mysql

centos 7 后 安装 mariadb

安装:

yum install mariadb 
yum install mariadb-server

检查:

mysql --version

开启启动:

systemctl enable mariadb 

设定初始密码:

# systemctl start mariadb
# mysqladmin -u root password '123456'

重新设定密码:

开启安全模式:

# systemctl stop mariadb
# mysqld_safe --skip-grant-tables&
# mysql --user=root mysql
> update user set Password=PASSWORD('new-password');
> flush privileges;
> exit;
# killall mysqld_safe&
# systemctl start mariadb
  • 停止服务
  • root权限开启mysql安全模式
  • 进入mysql数据库
  • 更新表,刷新,退出
  • 杀死mysqld 安全进程
  • 开启mysql服务

配置:

文件:

/etc/my.cnf

PHP

 # 安装EPEL yum存储库
 yum install epel-release -y
 # 安装Remi存储库
 rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
 # 安装 PHP 7.3 
 yum --enablerepo=remi-php73 install php
 # 安装 PHP 7.2 
 yum --enablerepo=remi-php72 install php
 # 安装 PHP 7.1 
 yum --enablerepo=remi-php71 install php

安装:

yum install php php-fpm php-gd php-mcrypt php-pear php-mysql php-mysqlite
##或者 
yum install php php-*

检查版本:

$ php -v 

服务:

# service php-fpm start|restart|stop

开启启动:

chkconfig php-fpm on

配置:

文件

/etc/php.ini       
/etc/php.d/        
/etc/php-fpm.conf
/etc/php-fpm.d/  

/etc/php.ini 配置要点

; 时间区域
date.timezone = "Asia/Shanghai"

必要的库,有的在对应的 /etc/php.d/ 文件夹中

;extension=iconv.so
;extension=mcrypt.so    
;extension=sockets.so
;extension=soap.so

/etc/php-fpm.conf

/etc/php-fpm.d/www.conf fpm的配置文件修改要点

端口

listen = 127.0.0.1:9000
; listen = /var/run/php-fpm/php-fpm.sock

用户写入文件的 user:group 和权限

listen.owner = nginx
listen.group = nginx
listen.mode = 0666  
user = nginx
group = nginx

执行时限**

request_terminate_timeout = 180s

权限:

对session.savepath 的组用户写权限

# chown root:nginx -R /var/lib/php/

nginx

服务器部署可替代 Apache,Caddy。

添加内容到 /etc/yum.repos.d/CentOS-Base.repo

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

安装:

yum install nginx

开启启动:

chkconfig nginxd on

查看:

nginx -v 

配置:

/etc/nginx/conf.d/

以 fb.geely.com 为模板

server {
  listen       80;
  server_name  fb.geely.com;
  charset utf-8;
  root        /home/nginx/fb.geely.com;
  access_log  /home/nginx/logs/fb.geely.com.log  main;
  error_log   /home/nginx/logs/fb.geely.com_error.log;

  location /{
    index  index.php index.html index.htm;
    # 隐藏index.php入口文件
    if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php/$1 last;
    }
  }

  location ~ ^.+\.php{
    fastcgi_buffer_size 128k;
    fastcgi_buffers 32 32k;
    #fastcgi_pass  unix:/var/run/php-fpm/php-fpm.sock;
    fastcgi_pass  127.0.0.1:9000; 
    fastcgi_index index.php;
    fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    include       fastcgi_params;
  }

  # 禁止访问 控制器等
  location ^~ /libs { deny all; }
  location ^~ /site { deny all; }
  # 迁移 bocadmin/index.php 后可禁止访问bocadmin
  #location ^~ /bocadmin { deny all; }
  
  location ^~ /static {access_log   off;}

  location ^~ /upload/ {
    #root /path/upload; # 修改路径
    access_log   off;
  }

  #error_page  404              /404.html;
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

SSL HTTPS 配置模板

server {
  listen       443 ssl;
  server_name  fb.geely.com;

  # ssl on;
  ssl_certificate     /home/nginx/fb.geely.com/ssl/fb.geely.com.crt;
  ssl_certificate_key /home/nginx/fb.geely.com/ssl/fb.geely.com.key;

  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout  5m;

  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers   on;

  root        /home/nginx/fb.geely.com;

  access_log  /home/nginx/logs/fb.geely.com.log  main;
  error_log   /home/nginx/logs/fb.geely.com_error.log;
	
  # 同上服务配置
}

参看 SSL CA证书

安全

自动检测服务并重启脚本:

#!/bin/bash
# file path /home/jibun/shell/checkservice.sh

service_log=/home/jibun/logs/server.log

ng=`service nginx status|awk '{print $5}'`
php=`service php-fpm status|awk '{print $5}'`
mysql=`service mysqld status|awk '{print $5}'`

if [[ $ng != *"running"* ]]; then
echo "---------------------------------" >> $service_log
date '+%Y-%m-%d %H:%M' >> $service_log
service nginx start >> $service_log
fi

if [[ $php != *"running"* ]]; then
echo "---------------------------------" >> $service_log
date '+%Y-%m-%d %H:%M' >> $service_log
service php-fpm start >> $service_log
fi

if [[ $mysql != *"running"* ]]; then
echo "---------------------------------" >> $service_log
date '+%Y-%m-%d %H:%M' >> $service_log
service mysqld start >> $service_log
fi

注意 mysql的服务默认为 mysqld ,有部分为mysql,自行斟酌

创建普通用户

创建用户jibun并创建密码

useradd -G nginx -g users jibun 
passwd jibun 

服务器路径

创建服务器文件路径。

/home/nginx         # nginx服务器所在文件夹
/home/nginx/logs    # 存放日志
/home/nginx/domain  # 对应的项目

执行过程

# cd home
# mkdir nginx 
# chown nginx:nginx -R nginx
# chmod g+w nginx
# cd nginx 
# mkdir logs
# su jibun
$ mkdir www.domain.com

上传对应的 www.domain.com 文件夹即可。

用户jibun文件夹:

/home/jibun            # home文件夹
/home/jibun/shell      # 定时执行脚本
/home/jibun/backdir    # 备份位置
/home/jibun/gitsource  # git源库(防篡改之用)

参看服务器方案