Linux CentOS7 系统中部署 Nginx + PHP-fpm + MySQL 环境
LNMP
由于 Nginx 轻量与高效的性能,Linux + Nginx + Mysql + PHP 的部署模式,被更多的应用于 PHP 的开发与生产环境中。
本文介绍 Nginx + php-fpm 的部署模式的安装与配置过程,先介绍下几个概念
cgi
是 web 服务器如 Apache、Nginx 等与 php 解释器之间的通信协议fast-cgi
是 cgi 的改良版本。相较于 cgi 的每次请求过来都 fork 一个进程,结束后 kill 这个进程,fast-cgi 会先 fork 一个 master ,然后再 fork 多个 worker,当请求过来时, master 会传递给一个 worker ,然后立即可以接受下一个请求。这样避免了重复的劳动,大大提高了效率php-cgi
是 PHP 5.4之前官方自带的实现 fast-cgi 协议的解释器,但由于性能的问题,被广为诟病,PHP 5.4后被 php-fpm 代替php-fpm
是 PHP 5.4之前第三方的管理 fast-cgi 进程的管理器,之后被官方收录,成为替代 php-cgi 的管理器和解释器
以上概念容易混淆,可自行查阅更详细的介绍。以下是整个安装过程。
SELinux
SELinux 是 Linux 内置的安全模块,是为了减小系统中服务进程可访问的资源,如果开启会导致服务访问出现异常等情况,一些云平台,如阿里云等,默认情况下,ECS 里面的 SELinux 是关闭的。我们可以手动关闭。
查看是否开启:
/usr/sbin/sestatus -v
临时关闭:
setenforce 0
永久关闭:
vi /etc/selinux/config
将 SELINUX=enforcing
改为SELINUX=disabled
设置后需要重启服务器才能生效
NGINX
安装
更新系统包和安装软件包,然后安装 Nginx
yum update -y
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install nginx1w
配置
编辑配置文件
vi /etc/nginx/nginx.conf
做如下修改
# `·worker_processes 1;` 修改成服务器内核数
worker_processes 8;
# `index index.html index.htm;` 增加默认文件
index index.html index.htm index.php;
# 修改 `server` 如下
server {
listen 80;
server_name localhost;
root /home/wwwroot/public;
client_max_body_size 500M;
server_tokens off;
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.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_split_path_info ^(.+\.php)(\/?.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
}
开启 gzip
,在 /etc/nginx/conf.d
中加入 gzip.conf
文件,如下
gzip on;
gzip_disable “MSIE [1-6].(?!.*SV1)”;
gzip_http_version 1.1;
gzip_vary on;
gzip_proxied any;
gzip_min_length 5000;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_types text/css text/xml text/plain application/json application/javascript application/xml application/rss+xml application/xhtml+xml;
启动 Nginx ,并加入开机自启动
systemctl start nginx
systemctl enable nginx
PHP
安装
yum
安装php-fpm
和相关扩展
yum install php72w-fpm php72w-opcache php72w-mysqlnd php72w-cli php72w-mbstring php72w-gd php72w-xml php72w-zip php72w-pecl-redis
配置
编辑配置文件
vi /etc/php-fpm.d/www.conf
做如下修改
# user = apache 修改为
user = nginx
# group = apache 修改为
group = nginx
启动 php-fpm ,并加入开机自启动
systemctl start php-fpm
systemctl enable php-fpm
MYSQL
安装
卸载centos7中自带的Mariadb
# 查询出来已安装的`Mariadb`
rpm -qa|grep mariadb
# 卸载`mariadb`,文件名为上述命令查询出来的文件
rpm -e --nodeps 程序名
查看是否已经安装了MySQL
rpm -qa | grep -i mysql
安装MySQL
服务
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql-server
启动
启动 MySQL
,并加入开机自启动
systemctl start mysqld
systemctl enable mysqld
修改默认root
密码
首先用系统默认密码登录数据库
# 查看默认密码
grep 'temporary password' /var/log/mysqld.log
# 如显示如下,则密码是`#<eBwq;r)6f1`
2019-08-22T10:11:04.849557Z 1 [Note] A temporary password is generated for root@localhost: #<eBwq;r)6f1
登录数据库,并修改密码
mysql -u root -p
# 密码强度满足:数组、字母、大小写、特殊字符
set password for 'root'@'localhost'=password('K!7F9KTk*0eq');
FTP
安装
# 查看是否已安装
rpm -qa | grep vsftpd
# 安装
yum -y install vsftpd
# 配置用户,如网站根目录为 `/home/wwwroot`
useradd -s /sbin/nologin -d /home/wwwroot wwwuser
chown wwwuser:wwwuser /home/wwwroot -R
# 修改用户密码
passwd wwwuser
配置
编辑配置文件
vi /etc/vsftpd/vsftpd.conf
修改如下
anonymous_enable=NO
userlist_enable=NO
allow_writeable_chroot=YES
chroot_local_user=YES
编辑文件
vi /etc/pam.d/vsftpd
修改
# 在以下配置行前加 `#`注释
auth required pam_shells.so
启动
启动 FTP
,并加入开机自启动
systemctl start vsftpd
systemctl enable vsftpd
REDIS
Redis
是存储在内存中的键值数据库,常被用作缓存应用,可选择安装
安装
yum -y install redis
配置
添加密码验证,修改配置文件
vi /etc/redis.conf
做如下修改
# 找到注释:`#requirepass foobared`,在其下一行添加如下配置
requirepass 23z2MmTGYow%
# 其中,`23z2MmTGYow%` 即为数据库密码
启动
启动 Redis
,并加入开机自启动
systemctl start redis
systemctl enable redis
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: