LNMP动态⽹站
LNMP动态⽹站
安装LNMP架构
yum安装 nginx1.24.0 php7.2 Mriadb5.7
1.安装 Nginx
//1.使⽤Nginx官⽅提供的rpm包
[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
//2.执⾏yum安装
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
2.使⽤第三⽅扩展epel源安装php7.2
//移除旧版php
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common
//安装扩展源
[root@nginx ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@nginx ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
//安装php72版本
[root@nginx ~]# yum -y install php72w php72w-cli php72w-common php72w-devel \
php72w-embedded php72w-gd php72w-mbstryuan php72w-pdo php72w-xml php72w-fpm \
php72w-mysqlnd php72w-opcache --nogpgcheck
//启动php
[root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm
3.安装 Mariadb
//下载官⽅扩展源, 扩展源集成mysql5.6、 5.7、 8.0,仅5.7仓库是开启
[root@nginx ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
[root@nginx ~]# yum install mysql-community-server -y --nogpgcheck
[root@nginx ~]# systemctl start mysqld
[root@nginx ~]# systemctl enable mysqld
[root@nginx ~]# systemctl status mysqld
//如果mysql登陆需要密码,请查看该⽂件
[root@nginx ~]# grep 'temporary password' /var/log/mysqld.log
2023-06-03T05:54:37.607361Z 1 [Note] A temporary password is generated for
root@localhost: Fk%:(-KCC3St
//登陆mysql重新配置密码
[root@nginx ~]# mysql -uroot -p'123456'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> flush privileges;
#登不上试试以下方法
[root@linuxprobe mysql]# mysql -h 127.0.0.1 -p123456
#CREATE USER `root`@`127.0.0.1` IDENTIFIED BY '123456';
Nginx与PHP原理
Nginx FastCGI的运⾏原理
nginx fastcgi`访问`php
fastcgi 协议是⼀种标准化的协议,⽤于定义 Web 服务器和应⽤程序之间的通信⽅式。 nginx 和 PHP-FPM 之间
使⽤ fastcgi 协议来进⾏通信,并通过请求和响应消息传递数据。
1.⽤户发送http请求报⽂给nginx服务器
2.nginx会根据⽂件url和后缀来判断请求
3.如果请求的是静态内容,nginx会将结果直接返回给⽤户
4.如果请求的是动态内容,nginx会将请求交给fastcgi客户端,通过fastcgi_pass将这个请求发送给php-fpm
5.php-fpm收到请求后会通过本地监听的socket交给wrapper
6.wrapper收到请求会⽣成新的线程调⽤php动态程序解析服务器
7.如果⽤户请求的是博⽂、或者内容、 PHP会请求MySQL查询结果
8.如果⽤户请求的是图⽚、附件、 PHP会请求nfs存储查询结果
9.php会将查询到的结果交给Nginx
10.nginx会⽣成⼀个响应报⽂返还给⽤户
PHP配置文件优化
php-ini优化
//打开php的安全模式,控制php执⾏危险函数, 默认是Off,改为On
sql.safe_mode = Off
//关闭php头部信息, 隐藏版本号, 默认是On,该为Off
expose_php = On
//错误信息输出控制
display_error = Off
error_reportyuan = E_WARNING & E_ERROR
//记录错误⽇志⾄后台, ⽅便追溯
log_errors = On
error_log = /var/log/php_error.log
//每个脚本时间最⼤内存
memory_limit = 128M
//上传⽂件最⼤许可,默认2M, 建议调整为16,32M
upload_max_filesize = 2M
//禁⽌远程执⾏phpshell,默认On, 建议Off
allow_url_fopen = On
//时区调整,默认PRC, 建议调整为Asia/Shanghai
date.timezone = PRC
//整体优化后配置⽂件
sql.safe_mode = Off
expose_php = Off
display_error = Off
error_reportyuan = E_WARNING & E_ERROR
log_errors = On
error_log = /var/log/php_error.log
upload_max_filesize = 50M
allow_url_fopen = Off
date.timezone = Asia/Shanghai
php-fpm优化
PHP-FPM配置⽂件 4核16G、 8核16G
[root@nginx ~]# cat /etc/php-fpm.d/www.conf
[global]
pid = /var/run/php-fpm.pid
#php-fpm程序错误⽇志
error_log = /var/log/php/php-fpm.log
log_level = warnyuan
#进程打开⽂件的软限制,⽤来限制 PHP-FPM 进程可以同时打开的最⼤⽂件数量。
rlimit_files = 655350
#PHP-FPM 应⽤程序使⽤的事件机制,可以选⽤ select、 poll 和 epoll 三种机制。
events.mechanism = epoll
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.process_idle_timeout = 15s;
pm.max_requests = 2048
#php-www模块错误⽇志
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/php-www.log
php_admin_flag[log_errors] = on
#php慢查询⽇志
request_slowlog_timeout = 5s
slowlog = /var/log/php/php-slow.log
PHP5-FPM配置详解释
[global]
#pid设置, 记录程序启动后pid
pid = /var/run/php-fpm.pid
#php-fpm程序启动错误⽇志路径
error_log = /soft/log/php/php-fpm_error.log
# 错误级别. 可⽤级别为: alert(必须⽴即处理) ,error(错误情况) , warnyuan(警告情况) , notice(⼀般
重要信息) , debug(调试信息) . 默认: notice.
log_level = warnyuan
#进程打开⽂件的软限制,⽤来限制 PHP-FPM 进程可以同时打开的最⼤⽂件数量
rlimit_files = 65535
#PHP-FPM 应⽤程序使⽤的事件机制,可以选⽤ select、 poll 和 epoll 三种机制。
events.mechanism = epoll
#启动进程的⽤户和组
[www]
user = www
group = www
# fpm监听端⼝
listen = 127.0.0.1:9000
# unix socket设置选项,如果使⽤tcp⽅式访问,这⾥注释即可。
listen.owner = www
listen.group = www
# 允许访问FastCGI进程的IP, any不限制
listen.allowed_clients = 127.0.0.1
# pm设置动态调度
pm = dynamic
# 同⼀时刻最⼤的php-fpm⼦进程数量
pm.max_children = 200
# 动态⽅式下的起始php-fpm进程数量
pm.start_servers = 20
# 动态⽅式下服务器空闲时最⼩php-fpm进程数量
pm.min_spare_servers = 10
# 动态⽅式下服务器空闲时最⼤php-fpm进程数量
pm.max_spare_servers = 30
# 最⼤请求
pm.max_requests = 1024
pm.process_idle_timeout = 15s;
# FPM状态⻚⾯,⽤于监控php-fpm状态使⽤
pm.status_path = /status
# 错误⽇志
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on
# 配置php慢查询, 以及慢查询记录⽇志位置
request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log