浏览量: 470
CentOS 7 与前代相比有了巨大改变, 服务管理器, 时间设置等等, 对于习惯了前代版本的人来说还是需要时间适应的.
下面讲一下如何从头配置 CentOS 生产环境, 以 DigitalOcean 云主机为例, 有关此云主机的介绍 请点这里
CentOS 7 为64位系统, 最低内存要求512M.
一. 添加常用软件源
Remi 主打 php 及相关扩展, 所以安装 php 这个源是不二选择.
首先进入 Remi 网站, 在 Maintained Enterprise Linux (RHEL / CentOS / Other clones) 列表中找到 Enterprise Linux 7 项后面的 remi-release-7.rpm , 执行下面的命令安装.
yum install http : / / mirror .innosol .asia / remi / enterprise / remi - release - 7.rpm
编辑 remi.repo 文件
nano / etc / yum .repos .d / remi .repo
修改 [remi]
保存退出
编辑 remi-php72.repo
nano / etc / yum .repos .d / remi - php72 .repo
修改 [remi-php72]
保存退出
一般来说, 安装 Remi 源的时候会附带安装 EPEL 源, 如果没有安装, 则按以下方法安装.
EPEL 包含丰富的软件, 进入网站往下拉, 找到 Quickstart 项, 根据系统架构与版本选 RHEL/CentOS 7 , 点击, 系统会根据来访ip查找最快的源镜像, 国内访问通常会转到搜狐与中科大的源.如果想用美国源, 最好是用代理访问或者在服务器中输入如下命令
yum install https : / / dl .fedoraproject .org / pub / epel / epel - release - latest - 7.noarch.rpm
进入 Nginx 官网 , 点右侧的 download 链接, 拉到最下面找到 Pre-Built Packages 项. 点 mainline version(主线版本; stable version 为稳定版本) 版本的链接. 根据提示编辑 repo 文件的内容, 具体操作如下.
在 yum repo 目录创建新的 nginx.repo 文件
nano / etc / yum .repos .d / nginx .repo
输入以下内容
[ nginx ]
name = nginx repo
baseurl = http : / / nginx .org / packages / mainline / centos / 7 / $basearch /
gpgcheck = 0
enabled = 1
保存退出
MariaDB 是 MySql 分支出来的项目, 因为 MySql 被 Oracle 收购且闭源, 所以有了 MariaDB, 并逐步添加新的功能. 最新版 10.x 已不再从 MySql 合并代码.
打开页面 https://downloads.mariadb.org/mariadb/repositories/#mirror=qiming&distro=CentOS ,选择最新版, 将下面的内容保存为 /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.3 CentOS repository list - created 2018-09-14 06:53 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[ mariadb ]
name = MariaDB
baseurl = http : / / yum .mariadb .org / 10.3 / centos7 - amd64
gpgkey = https : / / yum .mariadb .org / RPM - GPG - KEY - MariaDB
gpgcheck = 1
更新 yum, 至此软件源添加完毕.
二. 配置生产环境
1. 安装 nginx
#安装nginx官方版本
yum install nginx
#或安装带 Pagespeed 的 nginx
yum install ulyaoth - nginx - mainline - pagespeed
systemctl enable nginx #随系统启动
systemctl start nginx #启动服务
2. 安装 php
yum install php php - fpm php - mysql php - pdo php - gd php - intl php - bcmath php - cli php - mbstring php - mcrypt php - pecl - zip
systemctl start php - fpm #启动服务
systemctl enable php - fpm #随系统启动
安装 Zend Opcache 缓存扩展
如果 httpd 服务启动了, 停用此服务.
systemctl disable httpd #禁止随系统启动
systemctl stop httpd #停用服务
为了避免 nginx 与 php-fpm 使用中出现权限不足的问题(例如 wordpress 自动更新时要求授权), 建议将 nginx 与 php-fpm 的执行帐户统一, 且不允许该帐户登录系统, 且只允许操作 html 目录.
查看当前所有用户和用户组
#查看用户
cat / etc / passwd
#查看用户组
cat / etc / group
添加一个web相关服务公用帐户并创建一个公用组, 以备后面使用.
#新建名为 webroot 的用户组
groupadd webroot
#添加一个名为 webroot 的帐户, 不允许登录系统. 并加入到 webroot 用户组.
useradd - g webroot - s / sbin / nologin webroot
#查看某用户所属的用户组
groups webroot
将 nginx 的 html 目录所有者改为 webroot, 用户组改为 webroot
chown - R webroot : webroot / usr / share / nginx / html
配置 Nginx
nano / etc / nginx / nginx .conf
修改为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#上一步中新增的用户名
user webroot ;
#nginx 使用的线程数, cpu核数 * 2 最佳.
worker _ processes 2 ;
#并发请求时使用线程的顺序, 用二进制位表示.
worker_cpu _ affinity 01 10 ;
error_log / var / log / nginx / error .log warn ;
pid / var / run / nginx .pid ;
events {
#最大连接数
worker _ connections 1024 ;
#允许 Nginx 在已经得到一个新连接的通知时接收尽可能更多的连接
multi_accept on ;
}
http {
include / etc / nginx / mime .types ;
default_type application / octet - stream ;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' ;
access_log / var / log / nginx / access .log main ;
sendfile on ;
#tcp_nopush on;
keepalive _ timeout 65 ;
#启用 gzip 压缩
gzip on ;
gzip_vary on ;
gzip_http _ version 1.1 ;
gzip_min _ length 1k ;
gzip _ buffers 4 8k ;
gzip_comp _ level 2 ;
gzip_types text / plain text / css
application / x - javascript text / xml
application / xml application / xml + rss
text / javascript application / javascript
image / svg + xml ;
include / etc / nginx / conf .d / * .conf ;
}
各选项说明
gzip on;
该指令用于开启或关闭gzip模块(on/off)
gzip_http_version 1.1;
识别http的协议版本(1.0/1.1)
gzip_min_length 1k;
设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_buffers 4 8k;
设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。 例如 4 4k 代表以4k为单位,按照原始数据大小以4k为单位的4倍申请内存。 4 8k 代表以8k为单位,以8k为单位的4倍申请内存。
gzip_comp_level 2;
gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_types text/plain text/css
application/x-javascript text/xml
application/xml application/xml+rss
指定要压缩的 mime 类型. 无论是否指定, ”text/html” 类型总是会被压缩的.
gzip_vary on;
由于客户端和服务端之间可能存在一个或多个中间实体(如缓存服务器),有些实现得有 BUG 的缓存服务器,会忽略响应头中的 Content-Encoding,从而可能给不支持压缩的客户端返回缓存的压缩版本.
有两个方案可以避免这种情况发生:
将响应头中的 Cache-Control 字段设为 private,告诉中间实体不要缓存它.
增加 Vary: Accept-Encoding 响应头,明确告知缓存服务器按照 Accept-Encoding 字段的内容,分别缓存不同的版本.
nginx 使用第二种方案.
启用 https
请参考 <<CentOS 7 Nginx Let’ s Encrypt SSL 证书安装配置>>
设置默认虚拟主机
nano / etc / nginx / conf .d / default .conf
找到 server 节中的 listen 参数, 在端口后面加入 default 表示此虚拟主机为默认值, 当找不到配置 server_name 虚拟主机时会使用这里的默认虚拟主机.
server {
listen 80 default ;
server _ name localhost ;
#以下省略
}
添加默认首页, 找到以下内容
location / {
root html ;
index index .html index .htm ;
}
将 root 提到 location 外层, 成为全局设置.
在 index 项中加入 index.php 类型
root / usr / share / nginx / html ;
location / {
index index .html index .htm index .php ;
}
使用 php-fpm
location ~ \ .php $ {
fastcgi _ pass 127.0.0.1 : 9000 ;
fastcgi_index index .php ;
fastcgi_param SCRIPT _ FILENAME $request_filename ;
#或者像下面这样也是可以的
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params ;
}
禁止访问 .htxxx 文件
location ~ / \ .ht {
deny all ;
}
设置静态文件缓存时间
在 location 下方加入下面的配置.
#设置静态文件过期时间为10天
location ~ \ . ( gif | jpg | jpeg | png | bmp | swf ) $
{
expires 10d ;
}
#设置js, css文件过期时间为1天
location ~ \ . ( js | css ) ? $
{
expires 1d ;
}
最后的样子如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
server {
listen 80 default ;
server_name localhost ;
root / usr / share / nginx / html ;
charset utf - 8 ;
#access_log /var/log/nginx/log/host.access.log main;
location / {
index index .php index .html index .htm ;
}
#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 $ {
fastcgi _ pass 127.0.0.1 : 9000 ;
fastcgi_index index .php ;
fastcgi_param SCRIPT _ FILENAME $request_filename ;
include fastcgi_params ;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ / \ .ht {
deny all ;
}
location ~ \ . ( gif | jpg | jpeg | png | bmp | swf ) $
{
expires 10d ;
}
location ~ \ . ( js | css ) ? $
{
expires 1d ;
}
}
保存退出
配置 php
将允许上传的最大值改为20M(根据需求定).
post_max_size = 20M
upload_max_filesize = 20M
配置 php-fpm
nano / etc / php - fpm .d / www .conf
修改用户名和用户组, 优化参数.
user = webroot
group = webroot
pm .max_children = 20
pm .start_servers = 5
pm .min_spare_servers = 5
pm .max_spare_servers = 20
pm .max_requests = 120
保存退出
重启 Nginx 与 php-fpm 服务
systemctl restart nginx
systemctl restart php - fpm
在 /usr/share/nginx/html 中新建 phpinfo.php 文件
nano / usr / share / nginx / html / phpinfo .php
保存退出
暂时关闭防火墙, 待所有服务配置完成后统一设置防火墙.
systemctl stop iptables
systemctl stop firewalld #如果使用 firewallD 防火墙, 则是这样关闭
如能在浏览器中使用 ip 访问, 说明安装成功.
3. 安装 MariaDB 数据库
yum install mariadb - server mariadb - client
如果出示包冲突, 可能是因为已经安装了 mysql 相关的包, 应先卸载.
然后再次安装即可.
systemctl enable mariadb #随系统启动
systemctl start mariadb #启动服务
查询当前所有用户
mysql
use mysql ;
select host , user , password from user ;
增加新用户:
grant 权限 on 数据库. * to 用户名@ 登录主机 identified by “密码”
如,增加一个用户user1密码为password1,让其可以在任何主机上登录, 并对所有数 据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令,注意不要忘了分号.
mysql
grant < select , insert , update , delete | all > on * . * to user1 @ '%' identified by "password1" ;
flush privileges ;
如果你不想user1有密码,可以再打一个命令将密码去掉。
mysql
grant < select , insert , update , delete | all > on * . * to user1 @ '%' identified by "" ;
flush privileges ;
quit
根据服务器配置优化 MariaDB
根据内存大小选择示例配置文件(位于 /usr/share/mysql/ 中, 以 my-xxxx.cnf 命名), 复制到 /etc/my.cnf.d/ 中, 然后高速参数配置. 我的服务器内存为 512M, 因此选择 my-large.cnf.
cp / usr / share / mysql / my - large .cnf / etc / my .cnf .d / myserver .cnf
nano / etc / my .cnf .d / myserver .cnf
去掉 innodb 前的”#”号以启用 InnoDB 引擎, 优化各项参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[ client ]
default - character - set = utf8
[ mysqld ]
#将默认引擎改为 innodb
default - storage - engine = innodb
character - set - server = utf8
port = 3306
socket = / var / lib / mysql / mysql .sock
skip - external - locking
key_buffer_size = 96M
max_allowed_packet = 4M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 2
innodb_data_home_dir = / var / lib / mysql
innodb_data_file_path = ibdata1 : 10M : autoextend
innodb_log_group_home_dir = / var / lib / mysql
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 96M
innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
#日志先写到磁盘缓存, 再写到磁盘, 而不是强制写入磁盘
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
保存退出, 重启 MariaDB.
systemctl restart mariadb
如果遇到无法启动的情况, 可能是因为 InnoDB 中设置的日志文件大小与实际存在的大小不一至. 因为这里是新安装的数据库, 所以直接删除已经存在的日志文件, 重启后会生成新的. 如果是有重要数据的数据库, 则应修改配置值为原文件相应大小.
#删除已经存在的数据库文件
rm / var / lib / mysql / ib_logfile *
#重新启动
systemctl restart mariadb
4. 安装 vsFTP
创建vsftp 使用的本地帐号, 禁止在本地登录.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#创建一个无法登录shell的用户
useradd - s / sbin / nologin vsftp
#将用户添加到 webroot 组
usermod - G vsftp , webroot vsftp
groups vsftp
#这里将 /usr/share/nginx 做为 ftp 根目录
#nginx 的默认属主为 root, 这里不改动.
#将 nginx 目录下所有文件和文件夹属主改为 vsftp, 组改为 webroot
chown - R vsftp : webroot / usr / share / nginx / *
#更改所有文件和文件夹权限为 664
chmod - R 664 / usr / share / nginx / *
#为所有文件夹增加x权限
find / usr / share / nginx / - type d - exec chmod 774 { } \ ;
#html 目录改回原来的属主
chown - R webroot : webroot / usr / share / nginx / html
编辑配置文件
nano / etc / vsftpd / vsftpd .conf
我们这里使用虚拟用户的形式, 配置如下.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
anonymous_enable = no #禁用匿名ftp
local_enable = yes #使用虚拟用户必须开启
write_enable = yes #允许任何ftp写入
local_umask = 022 #上传的文件的权限
dirmessage_enable = YES #启用目录信息
xferlog_enable = yes #启用日志
connect_from_port_20 = yes #使用20端口发起连接
ftpd_banner = Welcome to My FTP Server ^ _ ^ . #ftp连接欢迎语
chroot_local_user = YES #禁止用户浏览自己目录以外的目录
ls_recurse_enable = yes #启用循环目录结构, 系统性能够强才启用.
listen = no #64位系统中禁用仅ipv4监听
listen_ipv6 = YES #启用ipv6监听, 将同时监听ipv4和ipv6
pam_service_name = vsftpd #验证模块
userlist_enable = yes #禁止 user_list 文件中列出的用户登录
tcp_wrappers = yes #使用 tcp 防火墙
#以下是手动添加的
pasv_min_port = 30000 #被动模式中使用的最小端口
pasv_max_port = 35000 #被动模式中使用的最大端口
guest_enable = YES #启用虚拟用户
guest_username = vsftp #虚拟用户绑定的本地用户
virtual_use_local_privs = YES #虚拟用户和本地用户有相同权限
allow_writeable_chroot = YES #允许ftp根目录可写
user_config_dir = / etc / vsftpd / user _ conf #虚拟用户配置目录
生成vsftpd虚拟用户数据库文件
建立虚拟用户名单文件
输入以下内容
ftp1
password1
ftp2
password2
格式很简单:“一行用户名,一行密码!”。
生成虚拟用户数据文件
db_load - T - t hash - f / etc / vsftpd / ftpuser / etc / vsftpd / users .db
chmod 600 / etc / vsftpd / users .db
配置PAM验证文件
将原有内容注释或删除, 将以下内容加入到文件中.
#db=/etc/vsftpd/users , 这里的users 是上一步创建的数据库名, 去掉后缀.
auth required pam_userdb .so db = / etc / vsftpd / users
account required pam_userdb .so db = / etc / vsftpd / users
创建虚拟用户独立配置文件
每个虚拟用户必需有一个同用户名相同的配置文件, 这个文件里保存着对这个用户的个性化配置, 里面的属性可以是 vsftpd.conf 中的任何属性.
创建虚拟用户配置文件目录, 这个目录是在 vsftpd.conf 中 user_config_dir= 配置的.
mkdir / etc / vsftpd / user_conf
创建用户配置文件,以用户名命名.
nano / etc / vsftpd / user_conf / ftp1
写入以下内容
local_root = / usr / share / nginx / html #当前用户的ftp根目录
idle_session_timeout = 600
data_connection_timeout = 120
max_clients = 10 #最大连接数
max_per_ip = 5 #最大线程数
local_max_rate = 50000 #最大带宽, 这里限制为50K
启动服务
systemctl enable vsftpd
systemctl start vsftpd
5. 允许通过防火墙
CentOS 7 改用 FirewallD 防火墙. 但目前资料相对较少, 有些复杂的问题不知如何配置, 所以还是改回 iptables.
首先禁用 firewalld , 启用 iptables.
yum install iptables - services
systemctl stop firewalld
systemctl disable firewalld
systemctl enable iptables
systemctl start iptables
systemctl enable ip6tables
systemctl start ip6tables
iptables 配置方法
刚安装完 iptables, 执行 iptables -L 命令, 查看默认配置, 看起来像下面的样子.
Chain INPUT ( policy ACCEPT ) #输入表
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED , ESTABLISHED #放行所有连入的正在建立连接或已建立连接的包
ACCEPT icmp -- anywhere anywhere #放行连入的icmp包
ACCEPT all -- anywhere anywhere #放行所有连入的包
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt : ssh #放行 ssh 包
REJECT all -- anywhere anywhere reject - with icmp - host - prohibited #拒绝所有连入的包
Chain FORWARD ( policy ACCEPT ) #转发表
target prot opt source destination
REJECT all -- anywhere anywhere reject - with icmp - host - prohibited #拒绝所有转发包
Chain OUTPUT ( policy ACCEPT ) #输出表
target prot opt source destination
iptables 的匹配规则是按从上至下的顺序判断的, 遇到 REJECT 如果匹配则停止查找规则. 所以要注意 REJECT 项在规则中的顺序, 避免影响其他规则.
我们把规则清除, 重新配置.
iptables - F #清除所有表的规则
#设置 INPUT、FORWARD、及 OUTPUT 表的缺省政策
iptables - P INPUT DROP #默认丢弃
iptables - P FORWARD DROP #默认丢弃
iptables - P OUTPUT ACCEPT #默认放行
iptables - A INPUT - i lo - j ACCEPT #允许本地网络访问
iptables - A INPUT - m state -- state ESTABLISHED , RELATED - j ACCEPT #放行所有正在建立连接或已建立连接的包
开放 ssh 服务
iptables - A INPUT - p tcp - m state -- state NEW - m tcp -- dport 22 - j ACCEPT #放行 ssh 端口
开放 http 服务
iptables - A INPUT - p tcp - m tcp -- dport 443 - j ACCEPT
iptables - A INPUT - p tcp - m tcp -- dport 80 - j ACCEPT
开放 ftp 服务
iptables - A INPUT - p tcp - m tcp -- dport 20 - j ACCEPT
iptables - A INPUT - p tcp - m tcp -- dport 21 - j ACCEPT
iptables - A INPUT - p tcp - m tcp -- dport 30000 : 35000 - j ACCEPT #被动模式中使用的端口范围
开放 mysql
iptables - A INPUT - p tcp - m tcp -- dport 3306 - j ACCEPT
保存配置
service iptables save #保存
systemctl restart iptables #重启服务
至此, 防火墙配置完成. 其实熟悉 iptables 命令后有一种更简单的配置方式–直接编辑 iptables 配置存储文件.
nano / etc / sysconfig / iptables
用以下内容覆盖原有内容, 注意规则顺序.
* filter
: INPUT DROP [ 0 : 0 ]
: FORWARD DROP [ 0 : 0 ]
: OUTPUT ACCEPT [ 0 : 0 ]
- A INPUT - m state -- state RELATED , ESTABLISHED - j ACCEPT
- A INPUT - p icmp - j ACCEPT
- A INPUT - i lo - j ACCEPT
- A INPUT - p tcp - m state -- state NEW - m tcp -- dport 22 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 443 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 80 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 20 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 21 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 30000 : 35000 - j ACCEPT
- A INPUT - p tcp - m tcp -- dport 3306 - j ACCEPT
- A FORWARD - j REJECT -- reject - with icmp - host - prohibited
COMMIT
保存退出, 重启服务.
systemctl restart iptables
如果是使用 firewalld 的, 配置方法如下, 使用 iptables 则可忽略.
查看 firewalld 中是否已经定义了 vsftpd 服务
firewall - cmd -- permanent -- get - services #列出永久选项所支持的服务
没有找到 vsftpd 服务, 那么我们手动新增一个 vsftpd 服务.
所有的系统预定义服务都在 /usr/lib/firewalld/services 中, 自定义服务则在 /etc/firewalld/services/ 中
先从预定义服务中随便copy 一个到 /etc/firewalld/services/ , 命名为 vsftp.xml
cp / usr / lib / firewalld / services / tftp .xml / etc / firewalld / services / vsftp .xml
编辑文件
修改名称,描述,协议和端口的定义, 删除不必要的内容. 得到如下所示.
<? xml version = "1.0" encoding = "utf-8" ?>
< service >
< short > vsFtp < / short >
< description > vsftpd < / description >
< port protocol = "tcp" port = "20" / >
< port protocol = "tcp" port = "21" / >
< / service >
保存退出
添加服务到当前区域
#永久开放vsftp
firewall - cmd -- permanent -- add - service = vsftp
#永久开放此端口范围
firewall - cmd -- permanent -- add - port = 30000 - 50000 / tcp
#重新加载配置,必要步骤.
firewall - cmd -- reload
#列出当前开放的服务与端口
firewall - cmd -- list - all
至此, ftp 服务配置完成.