pre.ctl { font-family: "Liberation Mono", monospace }
p { margin-bottom: 0.25cm; line-height: 120% }
a:link { }
部署LNMP环境
nginx[web服务,接收用户的请求] php [解释器] yum -y localinstall php-fpm-5.4.16<tab> [服务] mariadb [数据库客户端] mariadb-server [数据库服务器] mariadb-devel [依赖包] php-mysql [php连接mysql的扩展包]
所有服务
[[email protected] lnmp_soft]# netstat -nutlp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4595/nginx: master [[email protected] lnmp_soft]# netstat -nutlp |grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 9572/mysqld [[email protected] lnmp_soft]# systemctl restart php-fpm [[email protected] lnmp_soft]# netstat -nutlp |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 11156/php-fpm: mast
nginx+FastCGI 工作原理图:
工作流程: 1、web server 启动时载入fastCGI进程管理器 2、FastCGI进程管理器初始化,启动多个解释器进程 3、当客户端请求到达web server时,FastCGI进程管理器选择并连接一个解释器(php 9000) 4、FastCGI子进程完成处理后返回结果,将标准输出和错误信息从同一连返回web server FastCGI简介 FastCGI是一种常住型的CGI 将CGI解释器进程保持在内存中,进行委会与调度 FastCGI技术目前支持语言有PHP、C/C++、jave、Perl、Python、Ruby等 FastCGI缺点:内存消耗大 配置FastCGI
[[email protected] ~]# grep -v "^;" /etc/php-fpm.d/www.conf [www] listen = 127.0.0.1:9000 监听9000(php-fpm服务) listen.allowed_clients = 127.0.0.1 允许的客户地址 user = apache 用户 group = apache 用户组 pm = dynamic 模式 pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 slowlog = /var/log/php-fpm/www-slow.log php_admin_value[error_log] = /var/log/php-fpm/www-error.log php_admin_flag[log_errors] = on php_value[session.save_handler] = files php_value[session.save_path] = /var/lib/php/session
编辑nginx.conf
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;
地址重写 rewrite 旧 新 【选项】 redirect 地址栏变,临时重定向 permanent 地址栏变,永久重定向 last 停止执行其他rewrite break 停止执行其他语句,结束访问 rewrite /a.html /b.html last; rewrite /b.html /c.html last; rewrite /c.html /a.html last; 防止死循环 1:访问a.html跳转到b.html
vim /usr/local/nginx/conf/nginx.conf ... ... server { listen 80 server_name localhost; location / { rewrite a.html /b.html redirect; } #echo "BB" > /usr/local/nginx/html/b.html #nginx -s reload
2:访问192.168.4.5跳转到www.tmooc.cn
vim /usr/local/nginx/conf/nginx.conf ... ... server { listen 80 server_name localhost; location / { rewrite ^/ http://www.tmooc.cn; } 附加: 访问旧的网站页面,跳转到新的网站相同页面 rewrite ^/(.*) http://www.jd.com/$1; 保留和粘贴
3:不同浏览器访问相同页面返回结果不同
ie http://192.168.4.5/test.html firefox http://192.168.4.5/test.html uc http://192.168.4.5/test.html nginx【内置变量】 vim /usr/local/nginx/conf/nginx.conf server { ... ... if ($http_user_agent ~* curl){ rewrite ^/(.*) /curl/$1; } #cd /usr/local/nginx/html #echo "1" >test.html #mkdir curl #echo "2" >curl/test.html #nginx -s reload firefox http://192.168.4.5/test.html curl http://192.168.4.5/test.html
4:如果用户访问的页面不存则转到首页
vim /usr/local/nginx/conf/nginx.conf server { ... ... if (!-e $request_filename){ rewrite ^/ http://192.168.4.5; } #nginx -s reload
易错
1、php服务没有启动
2、nginx配置文件[动静分离] 出现下载页面
3、404 not found 没有此网页或网页位置放错
4、空白页面 脚本写错了
时间: 2024-08-13 09:41:13