最近开通了一台阿里云 ECS,系统使用 CentOS 7。这里把建站环境的配置过程记下来,主要包括 Zsh、MariaDB、Nginx、PHP-FPM 和 Redis。
下面的命令默认使用具有管理权限的账号执行。涉及用户名、密码、域名和公网地址的位置统一使用占位符,实际配置时按自己的环境替换。
配置 Zsh
安装 Zsh,并确认它已经写入可用 Shell 列表:
yum -y install zsh
cat /etc/shells把当前用户的默认 Shell 切换为 Zsh:
chsh -s /bin/zsh重新登录后检查:
echo "$SHELL"安装 Git 和 Oh My Zsh:
yum -y install git wget
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O /tmp/install-oh-my-zsh.sh
sh /tmp/install-oh-my-zsh.sh编辑 ~/.zshrc,例如把主题改为:
ZSH_THEME="ys"安装 MariaDB
CentOS 7 默认仓库提供 MariaDB:
yum -y install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installationmysql_secure_installation 会引导设置数据库管理员密码、移除匿名用户、限制管理员远程登录并删除测试库,根据提示逐项确认即可。
配置 UTF-8 字符集
编辑 /etc/my.cnf,在 [mysqld] 下加入:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake编辑 /etc/my.cnf.d/client.cnf:
[client]
default-character-set=utf8编辑 /etc/my.cnf.d/mysql-clients.cnf:
[mysql]
default-character-set=utf8重启 MariaDB 并检查结果:
systemctl restart mariadb
mysql -u root -pSHOW VARIABLES LIKE 'character%';
SHOW VARIABLES LIKE 'collation%';为博客创建独立数据库和账号,不要让业务账号拥有所有数据库的权限:
CREATE DATABASE blog DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'blog_user'@'localhost' IDENTIFIED BY '<strong-password>';
GRANT ALL PRIVILEGES ON blog.* TO 'blog_user'@'localhost';
FLUSH PRIVILEGES;如果确实需要远程连接,应把账号来源限制为可信 IP,同时检查 MariaDB 监听地址、CentOS 防火墙和阿里云安全组。不要直接向整个公网开放 3306 端口。
出现下面的错误,通常说明客户端根本没有连到数据库服务:
ERROR 2003 (HY000): Can't connect to MySQL server on '<server-ip>'可以依次检查服务状态、监听端口、防火墙、安全组以及数据库账号允许的来源地址。
安装 Nginx
添加 Nginx 官方仓库并安装:
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
systemctl start nginx
systemctl enable nginx安装 PHP-FPM
yum -y install php php-fpm php-mysql
systemctl start php-fpm
systemctl enable php-fpmPHP-FPM 的主要配置文件是 /etc/php-fpm.d/www.conf。修改后重启服务,并检查 9000 端口:
systemctl restart php-fpm
netstat -tln | grep 9000Nginx 需要把 PHP 请求转发给 PHP-FPM,相关 fastcgi_pass 地址必须与 PHP-FPM 的 listen 配置一致。
配置 Nginx 转发 PHP
先检查 /etc/php-fpm.d/www.conf 中的监听地址:
listen = 127.0.0.1:9000PHP-FPM 的 user 和 group 决定 PHP 脚本以哪个系统用户运行,不需要为了配合 Nginx 强行改成 nginx。保持软件包默认值即可,但站点目录和 Session 目录必须允许该用户访问。
编辑 /etc/nginx/conf.d/default.conf,加入 PHP 转发配置:
server {
listen 80;
server_name <domain>;
root /usr/share/nginx/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
access_log /var/log/nginx/blog_access.log combined;
}location ~ \.php$ 中的点需要转义,try_files $uri =404 可以避免把不存在的 PHP 文件交给 PHP-FPM。SCRIPT_FILENAME 使用 $document_root,这样修改站点根目录后不需要再写一份固定路径。
创建一个临时测试文件 /usr/share/nginx/html/info.php:
<?php
phpinfo();检查配置并重启服务:
nginx -t
systemctl restart php-fpm
systemctl restart nginx浏览器访问 http://<domain>/info.php,能够看到 PHP 信息说明 Nginx 与 PHP-FPM 已经连通。phpinfo() 会暴露服务器环境和扩展信息,验证完成后立即删除:
rm /usr/share/nginx/html/info.php安装 Redis 4.0.1
安装编译工具,下载并编译 Redis:
yum -y install gcc make
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make
make install准备配置和数据目录:
mkdir -p /etc/redis
mkdir -p /var/redis/data /var/redis/log /var/redis/run
cp redis.conf /etc/redis/redis.conf编辑 /etc/redis/redis.conf:
pidfile /var/redis/run/redis_6379.pid
logfile /var/redis/log/redis.log
dir /var/redis/data
daemonize yesRedis 默认没有为公网暴露而设计。保留本机绑定和保护模式,不要在安全组中直接开放 6379 端口。
复制 Redis 自带的启动脚本:
cp utils/redis_init_script /etc/init.d/redis
chmod +x /etc/init.d/redis检查脚本中的关键路径:
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.conf"在启动脚本开头补充 chkconfig 信息:
#!/bin/sh
# chkconfig: 2345 90 10注册并启动服务:
chkconfig --add redis
chkconfig redis on
systemctl start redisPHP Session 写入失败
站点退出登录时曾出现这类日志:
PHP Warning: Unknown: Failed to write session data (files).
Please verify that the current setting of session.save_path is correct
(/var/lib/php/session)先查看 php.ini 中的 session.save_path,再确认目录存在,并允许 PHP-FPM 的运行用户写入。CentOS 7 软件包中的 PHP-FPM 通常使用 apache 用户,可以这样处理:
install -d -o apache -g apache -m 700 /var/lib/php/session
systemctl restart php-fpm如果 www.conf 使用了其他用户,目录所有者也要相应调整。不要简单地把 Session 目录设成所有人可写。
至此,CentOS 7 上的基础建站环境就配置完成了。后面再根据站点需要补充 Nginx 虚拟主机、HTTPS、数据库备份和日志轮转等配置。