任务1 安装配置MySQL

安装MySQL

1.修改主机名

  1. [root@localhost ~]# hostnamectl set-hostname lnmp
  2. [root@localhost ~]# su

image.png
2.上传二进制面编译包至虚拟机
image.png

  1. [root@lnmp ~]# cd /usr/local/src/
  2. [root@lnmp src]# ls
  3. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz

image.png
3.安装常用工具

  1. [root@lnmp src]# yum install -y vim net-tools tree

image.png
4.解压安装包

  1. [root@lnmp src]# tar -zxvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz

5.移动

  1. [root@lnmp src]# mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql

image.png
6.创建MySQL用户,不可登录。启动需要

  1. [root@lnmp src]# useradd -s /sbin/nologin mysql
  2. [root@lnmp src]# cd /usr/local/mysql/

image.png
7.创建存放数据目录;更改权限

  1. [root@lnmp mysql]# mkdir -p /data/mysql
  2. [root@lnmp mysql]# chown -R mysql:mysql /data/mysql/
  3. [root@lnmp mysql]# cd /data/mysql/
  4. [root@lnmp mysql]# ls -la
  5. 总用量 0
  6. drwxr-xr-x 2 mysql mysql 6 9 3 17:28 .
  7. drwxr-xr-x 3 root root 18 9 3 17:28 ..
  8. [root@lnmp mysql]# cd /usr/local/mysql/

image.png
8.需要依赖

  1. [root@lnmp mysql]# yum install -y perl-Module-Install

image.png
9.初始化,上面有两个ok确定安装成功

  1. [root@lnmp mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/

image.png
出现报错缺少autoconf

  1. [root@lnmp mysql]# yum install -y autoconf

image.png
image.png
image.png

配置MySQL

1.修改配置文件

  1. [root@lnmp mysql]# cp support-files/my-default.cnf /etc/my.cnf
  2. cp:是否覆盖"/etc/my.cnf" y
  3. [root@lnmp mysql]# vim /etc/my.cnf
  4. basedir = /usr/local/mysql
  5. datadir = /data/mysql
  6. port = 3306
  7. server_id = 29
  8. socket = /tmp/mysql.sock

image.png
2.配置启动脚本

  1. [root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  2. [root@lnmp mysql]# chmod 777 /etc/init.d/mysqld
  3. [root@lnmp mysql]# vim /etc/init.d/mysqld
  4. basedir=/usr/local/mysql
  5. datadir=/data/mysql

image.png
image.png

  1. [root@lnmp mysql]# chkconfig --add mysqld
  2. [root@lnmp mysql]# chkconfig mysqld on
  3. [root@lnmp mysql]# service mysqld start
  4. Starting MySQL SUCCESS!

image.png

  1. [root@lnmp mysql]# netstat -ntlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1097/sshd
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1430/master
  6. tcp6 0 0 :::3306 :::* LISTEN 19604/mysqld
  7. tcp6 0 0 :::22 :::* LISTEN 1097/sshd
  8. tcp6 0 0 ::1:25 :::* LISTEN 1430/master

image.png

任务2 安装配置PHP

安装PHP

1.上传二进制面编译包至虚拟机
image.png

  1. [root@lnmp mysql]# cd /usr/local/src/
  2. [root@lnmp src]# ls
  3. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30.tar.gz

image.png
2.需要依赖

  1. [root@lnmp src]# yum install -y gcc
  2. [root@lnmp src]# yum install -y libxml2-devel
  3. [root@lnmp src]# yum install -y openssl
  4. [root@lnmp src]# yum install -y openssl-devell
  5. [root@lnmp src]# yum install -y bzip2
  6. [root@lnmp src]# yum install -y bzip2-devel
  7. [root@lnmp src]# yum install -y libpng
  8. [root@lnmp src]# yum install -y libpng-devel
  9. [root@lnmp src]# yum install -y freetype
  10. [root@lnmp src]# yum install -y freetype-devel
  11. [root@lnmp src]# yum install -y eplel-release
  12. [root@lnmp src]# yum install -y libmcrypt-devel
  13. [root@lnmp src]# yum install -y libcurl-devel
  14. [root@lnmp src]# yum install -y libjpeg-devel
  15. [root@lnmp src]# yum install -y epel-release
  16. [root@lnmp src]# tar -zxvf php-5.6.30.tar.gz
  17. [root@lnmp src]# cd php-5.6.30
  18. [root@lnmp php-5.6.30]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
  19. 进行编辑,时间会很久
  20. [root@lnmp php-5.6.30]# make && make install
  21. 检查成功出现0
  22. [root@lnmp php-5.6.30]# echo $?
  23. 0

image.png

配置PHP

1.复制配置文件

  1. [root@lnmp php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini

image.png
2.修改配置文件

  1. [root@lnmp php-5.6.30]# vim /usr/local/php-fpm/etc/php-fpm.conf
  2. [global]
  3. pid = /usr/local/php-fpm/var/run/php-fpm.pid
  4. error_log = /usr/local/php-fpm/var/log/php-fpm.log
  5. [zzx]
  6. listen = /tmp/php-fcgi.sock
  7. listen.mode = 666
  8. user = php-fpm
  9. group = php-fpm
  10. pm = dynamic
  11. pm.max_children = 50
  12. pm.start_servers = 20
  13. pm.min_spare_servers = 5
  14. pm.max_spare_servers = 35
  15. pm.max_requests = 500
  16. rlimit_files = 1024

image.png
3.带有successful 表示成功

  1. [root@lnmp php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
  2. [03-Sep-2021 22:37:59] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

image.png
4.启动php-fpm

  1. [root@lnmp php-5.6.30]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  2. [root@lnmp php-5.6.30]# chmod 755 /etc/init.d/php-fpm
  3. 创建php-fpm
  4. [root@lnmp php-5.6.30]# useradd -s /sbin/nologin php-fpm
  5. [root@lnmp php-5.6.30]# service php-fpm restart
  6. Gracefully shutting down php-fpm . done
  7. Starting php-fpm done
  8. [root@lnmp php-5.6.30]# ps aux |grep php-fpm
  9. root 36500 0.0 0.5 128132 5068 ? Ss 23:22 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  10. php-fpm 36501 0.0 0.4 128132 4816 ? S 23:22 0:00 php-fpm: pool zzx
  11. php-fpm 36502 0.0 0.4 128132 4816 ? S 23:22 0:00 php-fpm: pool zzx
  12. php-fpm 36503 0.0 0.4 128132 4816 ? S 23:22 0:00 php-fpm: pool zzx
  13. php-fpm 36504 0.0 0.4 128132 4816 ? S 23:22 0:00 php-fpm: pool zzx
  14. php-fpm 36505 0.0 0.4 128132 4820 ? S 23:22 0:00 php-fpm: pool zzx
  15. php-fpm 36506 0.0 0.4 128132 4820 ? S 23:22 0:00 php-fpm: pool zzx
  16. php-fpm 36507 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  17. php-fpm 36508 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  18. php-fpm 36509 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  19. php-fpm 36510 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  20. php-fpm 36511 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  21. php-fpm 36512 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  22. php-fpm 36513 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  23. php-fpm 36514 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  24. php-fpm 36515 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  25. php-fpm 36516 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  26. php-fpm 36517 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  27. php-fpm 36518 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  28. php-fpm 36519 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  29. php-fpm 36520 0.0 0.4 128132 4824 ? S 23:22 0:00 php-fpm: pool zzx
  30. root 36543 0.0 0.0 112660 964 pts/0 R+ 23:25 0:00 grep --color=auto php-fpm

任务3 安装配置nginx

建议选择一个稳定的版本进行安装。
image.png
1.上传安装包

  1. [root@lnmp ~]# cd /usr/local/src/
  2. [root@lnmp src]# ls
  3. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30
  4. nginx-1.17.8.tar.gz php-5.6.30.tar.gz
  5. [root@lnmp src]# tar -zxvf nginx-1.17.8.tar.gz
  6. [root@lnmp src]# cd nginx-1.17.8
  7. [root@lnmp nginx-1.17.8]# ./configure --prefix=/usr/local/nginx
  8. [root@lnmp nginx-1.17.8]# make && make install
  9. [root@lnmp nginx-1.17.8]# echo $?
  10. 0
  11. #因为文件比较小,安装速度很快。

image.png
image.png
2.编辑配置文件

  1. root@lnmp nginx-1.17.8]# vim /etc/init.d/nginx
  2. {
  3. echo -n $"Reloading $prog: "
  4. killproc -p $NGINX_PID $NGINX_SBIN -HUP
  5. RETVAL=$?
  6. echo
  7. return $RETVAL
  8. }
  9. restart()
  10. {
  11. stop
  12. start
  13. }
  14. configtest()
  15. {
  16. $NGINX_SBIN -c $NGINX_CONF -t
  17. return 0
  18. }
  19. case "$1" in
  20. start)
  21. start
  22. ;;
  23. stop)
  24. stop
  25. ;;
  26. reload)
  27. reload
  28. ;;
  29. restart)
  30. restart
  31. ;;
  32. configtest)
  33. configtest
  34. ;;
  35. *)
  36. echo $"Usage: $0 {start|stop|reload|restart|configtest}"
  37. RETVAL=1
  38. esac
  39. exit $RETVAL

image.png

  1. root@lnmp nginx-1.17.8]# chmod 755 /etc/init.d/nginx
  2. [root@lnmp nginx-1.17.8]# chkconfig --add nginx
  3. [root@lnmp nginx-1.17.8]# chkconfig nginx on
  4. [root@lnmp nginx-1.17.8]# > /usr/local/nginx/conf/nginx.conf
  5. [root@lnmp nginx-1.17.8]# vim /usr/local/nginx/conf/nginx.conf
  6. user nobody nobody;
  7. worker_processes 2;
  8. error_log /usr/local/nginx/logs/nginx_error.log crit;
  9. pid /usr/local/nginx/logs/nginx.pid;
  10. worker_rlimit_nofile 51200;
  11. events
  12. {
  13. use epoll;
  14. worker_connections 6000;
  15. }
  16. http
  17. {
  18. include mime.types;
  19. default_type application/octet-stream;
  20. server_names_hash_bucket_size 3526;
  21. server_names_hash_max_size 4096;
  22. ' $host "$request_uri" $status'
  23. ' "$http_referer" "$http_user_agent"';
  24. keepalive_timeout 30;
  25. client_header_timeout 3m;
  26. client_body_timeout 3m;
  27. send_timeout 3m;
  28. connection_pool_size 256;
  29. client_header_buffer_size 1k;
  30. large_client_header_buffers 8 4k;
  31. request_pool_size 4k;
  32. output_buffers 4 32k;
  33. postpone_output 1460;
  34. client_max_body_size 10m;
  35. client_body_buffer_size 256k;
  36. client_body_temp_path /usr/local/nginx/client_body_temp;
  37. proxy_temp_path /usr/local/nginx/proxy_temp;
  38. fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  39. fastcgi_intercept_errors on;
  40. tcp_nodelay on;
  41. gzip on;
  42. gzip_min_length 1k;
  43. gzip_buffers 4 8k;
  44. gzip_comp_level 5;
  45. gzip_http_version 1.1;
  46. gzip_types text/plain application/x-javascript text/css text/htm
  47. application/xml;
  48. server
  49. {
  50. listen 80;
  51. server_name localhost;
  52. index index.html index.htm index.php;
  53. root /usr/local/nginx/html;
  54. location ~ \.php$
  55. {
  56. include fastcgi_params;
  57. fastcgi_pass unix:/tmp/php-fcgi.sock;
  58. fastcgi_index index.php;
  59. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_nam
  60. e;
  61. }
  62. }
  63. }
  64. [root@lnmp nginx-1.17.8]# /usr/local/nginx/sbin/nginx -t
  65. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  66. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

image.png
3.启动服务

  1. [root@lnmp nginx-1.17.8]# service nginx start
  2. Starting nginx (via systemctl): [ 确定 ]
  3. [root@lnmp nginx-1.17.8]# ps aux |grep nginx
  4. root 39480 0.0 0.0 20508 636 ? Ss 00:06 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  5. nobody 39481 0.0 0.3 22904 3204 ? S 00:06 0:00 nginx: worker process
  6. nobody 39482 0.0 0.3 22904 3204 ? S 00:06 0:00 nginx: worker process
  7. root 39484 0.0 0.0 112660 964 pts/1 R+ 00:06 0:00 grep --color=auto nginx
  8. [root@lnmp nginx-1.17.8]# netstat -ntlp
  9. Active Internet connections (only servers)
  10. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  11. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39480/nginx: master
  12. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1097/sshd
  13. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1430/master
  14. tcp6 0 0 :::3306 :::* LISTEN 19604/mysqld
  15. tcp6 0 0 :::22 :::* LISTEN 1097/sshd
  16. tcp6 0 0 ::1:25 :::* LISTEN 1430/master

image.png

  1. [root@lnmp nginx-1.17.8]# curl localhost
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22. <p><em>Thank you for using nginx.</em></p>
  23. </body>
  24. </html>

image.png
4.测试

  1. #测试能否正确解析PHP
  2. root@lnmp nginx-1.17.8]# vim /usr/local/nginx/html/1.php
  3. <?php
  4. echo "test php scripts"
  5. ?>
  6. [root@lnmp nginx-1.17.8]# curl localhost/1.php
  7. test php scripts[root@lnmp nginx-1.17.8]#
  8. #测试成功

任务4 nginx配置

1.配置前先修改一下配置文件

  1. # 找到server删除下面这些
  2. server
  3. {
  4. listen 80;
  5. server_name localhost;
  6. index index.html index.htm index.php;
  7. root /usr/local/nginx/html;
  8. location ~ \.php$
  9. {
  10. include fastcgi_params;
  11. fastcgi_pass unix:/tmp/php-fcgi.sock;
  12. fastcgi_index index.php;
  13. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_nam
  14. e;
  15. }
  16. }
  1. # 最后添加一行,把server那一段删了,加入这一句 include vhost/*.conf;
  2. [root@lnmp nginx-1.17.8]#
  3. oot@lnmp nginx-1.17.8]# vim /usr/local/nginx/conf/nginx.conf
  4. user nobody nobody;
  5. worker_processes 2;
  6. error_log /usr/local/nginx/logs/nginx_error.log crit;
  7. pid /usr/local/nginx/logs/nginx.pid;
  8. worker_rlimit_nofile 51200;
  9. events
  10. {
  11. use epoll;
  12. worker_connections 6000;
  13. }
  14. http
  15. {
  16. include mime.types;
  17. default_type application/octet-stream;
  18. server_names_hash_bucket_size 3526;
  19. server_names_hash_max_size 4096;
  20. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  21. ' $host "$request_uri" $status'
  22. ' "$http_referer" "$http_user_agent"';
  23. sendfile on;
  24. tcp_nopush on;
  25. keepalive_timeout 30;
  26. client_header_timeout 3m;
  27. client_body_timeout 3m;
  28. send_timeout 3m;
  29. connection_pool_size 256;
  30. client_header_buffer_size 1k;
  31. large_client_header_buffers 8 4k;
  32. request_pool_size 4k;
  33. output_buffers 4 32k;
  34. postpone_output 1460;
  35. client_max_body_size 10m;
  36. client_body_buffer_size 256k;
  37. client_body_temp_path /usr/local/nginx/client_body_temp;
  38. proxy_temp_path /usr/local/nginx/proxy_temp;
  39. fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  40. fastcgi_intercept_errors on;
  41. tcp_nodelay on;
  42. gzip on;
  43. gzip_min_length 1k;
  44. gzip_buffers 4 8k;
  45. gzip_comp_level 5;
  46. gzip_http_version 1.1;
  47. gzip_types text/plain application/x-javascript text/css text/htm
  48. application/xml;
  49. include vhost/*.conf;
  50. }
  51. [root@lnmp nginx-1.17.8]# mkdir /usr/local/nginx/conf/vhost
  52. [root@lnmp nginx-1.17.8]# cd /usr/local/nginx/conf/vhost/
  53. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  54. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  55. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  56. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

image.png

一、默认虚拟主机

1.配置

  1. [root@lnmp vhost]# vim default.conf
  2. server
  3. {
  4. listen 80 default_server; # 有这个标记的就是默认虚拟主机
  5. server_name aaa.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/default;
  8. }
  9. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  10. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  11. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  12. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

image.png
image.png

2.测试

  1. [root@lnmp vhost]# mkdir -p /data/nginx/default
  2. [root@lnmp vhost]# echo " default server! " > /data/nginx/default/index.html
  3. # dingyi的为aaa.com
  4. [root@lnmp vhost]# curl -x127.0.0.1:80 bbb.com
  5. default server!
  6. [root@lnmp vhost]# curl -x127.0.0.1:80 aaa.com
  7. default server!

image.png
image.png

二、用户认证

1、整个域名认证

1.1、配置

  1. [root@lnmp vhost]# cd /usr/local/nginx/conf/vhost/
  2. [root@lnmp vhost]# ls
  3. default.conf
  4. [root@lnmp vhost]# vim test.com.conf
  5. server
  6. {
  7. listen 80;
  8. server_name test.com;
  9. index index.html index.htm index.php;
  10. root /data/nginx/test.com;
  11. location /
  12. {
  13. auth_basic "Auth";
  14. auth_basic_user_file /usr/local/nginx/conf/htpasswd;
  15. }
  16. }
  17. [root@lnmp vhost]# yum install -y httpd
  18. [root@lnmp vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd zzx
  19. New password: # 密码:0
  20. Re-type new password: # 密码:0
  21. Adding password for user zzx
  22. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload
  23. [root@lnmp vhost]# mkdir /data/nginx/test.com
  24. [root@lnmp vhost]# echo "test.com" > /data/nginx/test.com/index.html

image.png
1.2、测试

  1. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

image.png
在自己电脑修改hosts文件
C:\Windows\System32\drivers\etc
image.png

  1. [root@lnmp vhost]# curl -x127.0.0.1:80 test.com # 状态码为401说明需要验证
  2. <html>
  3. <head><title>401 Authorization Required</title></head>
  4. <body>
  5. <center><h1>401 Authorization Required</h1></center>
  6. <hr><center>nginx/1.17.8</center>
  7. </body>
  8. </html>
  9. [root@lnmp vhost]# curl -usxs:0 -x127.0.0.1:80 test.com # 正常访问状态码变为200,我这里报错是因为编译文件路径写错
  10. <html>
  11. <head><title>403 Forbidden</title></head>
  12. <body>
  13. <center><h1>403 Forbidden</h1></center>
  14. <hr><center>nginx/1.17.8</center>
  15. </body>
  16. </html>

2、针对目录认证

一般用来保护后台admin目录
2.1配置
针对目录做用户认证要修改location后面的路径

  1. root@lnmp vhost]# vim test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. location /admin/
  9. {
  10. auth_basic "Auth";
  11. auth_basic_user_file /usr/local/nginx/conf/htpasswd;
  12. }
  13. }
  14. [root@lnmp vhost]# mkdir /data/nginx/test.com/admin
  15. [root@lnmp vhost]# echo "asdfadmin" > /data/nginx/test.com/admin/index.html
  16. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  17. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  18. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  19. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

image.png
2.2测试
image.png

3、URL认证

针对URL做认证,即链接中带了某个关键字
3.1配置

  1. root@lnmp vhost]# vim test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. location ~ admin.php
  9. {
  10. auth_basic "Auth";
  11. auth_basic_user_file /usr/local/nginx/conf/htpasswd;
  12. }
  13. }

image.png
3.2测试

  1. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload
  5. [root@lnmp vhost]# curl -x127.0.0.1:80 test.com/admin.php
  6. <html>
  7. <head><title>401 Authorization Required</title></head>
  8. <body>
  9. <center><h1>401 Authorization Required</h1></center>
  10. <hr><center>nginx/1.17.8</center>
  11. </body>
  12. </html>

image.png

三、域名重定向

1、配置

  1. [root@lnmp vhost]# vim test.com.conf # 更改test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com test2.com test3.com; # 后面支持写多个域名,这里要和httpd的做一个对比
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. if ($host != 'test.com' ){
  9. rewrite ^(.*)$ http://test.com/$1 permanent; # 为永久重定向,状态码为301,如果写redirect则为302
  10. }
  11. }

image.png

2、测试

  1. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload
  5. # 状态码301就是域名重定
  6. [root@lnmp vhost]# curl -x127.0.0.1:80 test2.com
  7. <html>
  8. <head><title>301 Moved Permanently</title></head>
  9. <body>
  10. <center><h1>301 Moved Permanently</h1></center>
  11. <hr><center>nginx/1.17.8</center>
  12. </body>
  13. </html>
  14. [root@lnmp vhost]# curl -x127.0.0.1:80 test2.com -I
  15. HTTP/1.1 301 Moved Permanently
  16. Server: nginx/1.17.8
  17. Date: Fri, 03 Sep 2021 17:26:31 GMT
  18. Content-Type: text/html
  19. Content-Length: 169
  20. Connection: keep-alive
  21. Location: http://test.com//
  22. [root@lnmp vhost]# curl -x127.0.0.1:80 test2.com
  23. <html>
  24. <head><title>301 Moved Permanently</title></head>
  25. <body>
  26. <center><h1>301 Moved Permanently</h1></center>
  27. <hr><center>nginx/1.17.8</center>
  28. </body>
  29. </html>
  30. [root@lnmp vhost]# curl -x127.0.0.1:80 test2.com -I
  31. HTTP/1.1 301 Moved Permanently
  32. Server: nginx/1.17.8
  33. Date: Fri, 03 Sep 2021 17:37:31 GMT
  34. Content-Type: text/html
  35. Content-Length: 169
  36. Connection: keep-alive
  37. Location: http://test.com//

image.png
在windows上测试需要将两个域名都写入hosts文件,并使用没有缓存的浏览器。
image.png

四、nginx访问日志

image.png

1、配置

  1. # nginx 默认格式
  2. [root@lnmp vhost]# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
  3. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  4. ' $host "$request_uri" $status'
  5. ' "$http_referer" "$http_user_agent"';
  6. # combined_realip为日志格式名字,$remote_addr为网站的用户的出口IP;
  7. # $http_x_forwarded_for 为代理服务器的IP,如果使用了代理,则会记录IP
  8. # $time_local为当前时间;$host为主机名;$request_uri为访问的URL地址
  9. # $status为状态码,$http_referer为referer地址,$http_user_agent为user_agent
  10. root@lnmp vhost]# vim test.com.conf
  11. server
  12. {
  13. listen 80;
  14. server_name test.com;
  15. index index.html index.htm index.php;
  16. root /data/nginx/test.com;
  17. access_log /tmp/1.log combined_realip;
  18. }

image.png

2、测试

  1. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload
  5. [root@lnmp vhost]# curl -x127.0.0.1:80 test.com
  6. test.com
  7. # 每访问一次就会增加一条日志
  8. [root@lnmp vhost]# cat /tmp/1.log
  9. 127.0.0.1 - [04/Sep/2021:01:43:16 +0800] test.com "/" 200 "-" "curl/7.29.0"
  10. # 两种方法问
  11. 直接访问显示
  12. [root@lnmp ~]# tailf /tmp/1.log
  13. 127.0.0.1 - [04/Sep/2021:01:43:16 +0800] test.com "/" 200 "-" "curl/7.29.0"
  14. 127.0.0.1 - [04/Sep/2021:01:58:36 +0800] test.com "/" 200 "-" "curl/7.29.0"
  15. 127.0.0.1 - [04/Sep/2021:01:58:54 +0800] test.com "/" 200 "-" "curl/7.29.0"
  16. 网页访问显示
  17. 192.168.200.1 - [04/Sep/2021:02:00:20 +0800] test.com "//" 304 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38"
  18. 192.168.200.1 - [04/Sep/2021:02:00:20 +0800] test.com "/favicon.ico" 404 "http://test.com//" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38"

image.png
image.png

五、nginx日志切割

  1. # 自己写一个shell脚本
  2. [root@lnmp ~]# vim /usr/local/sbin/nginx_log_rotate.sh # 写入如下内容
  3. #!/bin/bash
  4. ##假设nignx的日志存放路径为/data/logs/
  5. d=`date -d "-1 day" +%Y%m%d`
  6. logdir="/tmp/"
  7. nginx_pid="/usr/local/nginx/logs/nginx.pid"
  8. cd $logdir
  9. for log in `ls *.log`
  10. do
  11. mv $log $log-$d
  12. done
  13. /bin/kill -HUP `cat $nginx_pid`
  14. [root@lnmp ~]# chmod 755 /usr/local/sbin/nginx_log_rotate.sh
  15. [root@lnmp ~]# crontab -e
  16. no crontab for root - using an empty one
  17. 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
  18. [root@lnmp ~]# ls /tmp/
  19. 1.log mysql.sock pear php-fcgi.sock
  20. [root@lnmp ~]# sh -x /usr/local/sbin/nginx_log_rotate.sh
  21. ++ date -d '-1 day' +%Y%m%d
  22. + d=20210903
  23. + logdir=/tmp/
  24. + nginx_pid=/usr/local/nginx/logs/nginx.pid
  25. + cd /tmp/
  26. ++ ls 1.log
  27. + for log in '`ls *.log`'
  28. + mv 1.log 1.log-20210903
  29. ++ cat /usr/local/nginx/logs/nginx.pid
  30. + /bin/kill -HUP 39480
  31. [root@lnmp ~]# ls /tmp/
  32. 1.log 1.log-20210903 mysql.sock pear php-fcgi.sock

image.png
image.png

六、配置静态文件不记录日志并添加过期时间

和LAMP一样,配置静态文件不记录日志,并添加过期时间。 目的是为了减少记录不必要的日志文件。缓存文件为了下次访问速度变快。

  1. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com test1.com test2.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. if ($host != 'test.com' ) {
  9. rewrite ^/(.*)$ http://test.com/$1 permanent;
  10. }
  11. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  12. {
  13. expires 7d;
  14. access_log off;
  15. }
  16. location ~ .*\.(js|css)$
  17. {
  18. expires 12h;
  19. }
  20. access_log /tmp/1.log combined_realip;
  21. }
  22. [root@lnmp ~]# echo '111' > /data/nginx/test.com/1.js
  23. [root@lnmp ~]# echo '222' > /data/nginx/test.com/2.jpg
  24. [root@lnmp ~]# touch /data/nginx/test.com/1.jss
  25. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  26. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  27. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  28. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
  29. [root@lnmp ~]# echo > /tmp/1.log
  30. [root@lnmp ~]# curl -I -x127.0.0.1:80 test.com/1.js
  31. HTTP/1.1 200 OK
  32. Server: nginx/1.17.8
  33. Date: Fri, 03 Sep 2021 18:48:07 GMT
  34. Content-Type: application/javascript
  35. Content-Length: 4
  36. Last-Modified: Fri, 03 Sep 2021 18:47:30 GMT
  37. Connection: keep-alive
  38. ETag: "61326dc2-4"
  39. Expires: Sat, 04 Sep 2021 06:48:07 GMT
  40. Cache-Control: max-age=43200
  41. Accept-Ranges: bytes
  42. [root@lnmp ~]# curl -I -x127.0.0.1:80 test.com/2.jpg
  43. HTTP/1.1 200 OK
  44. Server: nginx/1.17.8
  45. Date: Fri, 03 Sep 2021 18:48:15 GMT
  46. Content-Type: image/jpeg
  47. Content-Length: 4
  48. Last-Modified: Fri, 03 Sep 2021 18:47:36 GMT
  49. Connection: keep-alive
  50. ETag: "61326dc8-4"
  51. Expires: Fri, 10 Sep 2021 18:48:15 GMT
  52. Cache-Control: max-age=604800
  53. Accept-Ranges: bytes
  54. [root@lnmp ~]# cat /tmp/1.log
  55. 127.0.0.1 - [04/Sep/2021:02:48:07 +0800] test.com "/1.js" 200 "-" "curl/7.29.0"
  56. 127.0.0.1 - [04/Sep/2021:02:49:40 +0800] test.com "/1.jss" 200 "-" "curl/7.29.0"

image.png
image.png
image.png

七、Nginx防盗链

  1. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com test1.com test2.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. if ($host != 'test.com' ) {
  9. rewrite ^/(.*)$ http://test.com/$1 permanent;
  10. }
  11. location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|j
  12. peg|bmp|xls)$
  13. {
  14. expires 7d;
  15. valid_referers none blocked server_names *.test.com ;
  16. if ($invalid_referer) {
  17. return 403;
  18. }
  19. access_log off;
  20. }
  21. }
  22. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  23. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  24. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  25. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
  26. [root@lnmp ~]# curl -x127.0.0.1:80 -e "http://aaa.com/1.txt" test.com/2.jpg -I
  27. HTTP/1.1 403 Forbidden
  28. Server: nginx/1.17.8
  29. Date: Fri, 03 Sep 2021 18:52:41 GMT
  30. Content-Type: text/html
  31. Content-Length: 153
  32. Connection: keep-alive
  33. [root@lnmp ~]# curl -x127.0.0.1:80 -e "http://test.com/1.txt" test.com/2.jpg -I
  34. HTTP/1.1 200 OK
  35. Server: nginx/1.17.8
  36. Date: Fri, 03 Sep 2021 18:52:48 GMT
  37. Content-Type: image/jpeg
  38. Content-Length: 4
  39. Last-Modified: Fri, 03 Sep 2021 18:47:36 GMT
  40. Connection: keep-alive
  41. ETag: "61326dc8-4"
  42. Expires: Fri, 10 Sep 2021 18:52:48 GMT
  43. Cache-Control: max-age=604800
  44. Accept-Ranges: bytes

image.png
image.png

八、访问控制


1、针对目录进行访问控制


1.1 配置

  1. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com test1.com test2.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. access_log /tmp/1.log combined_realip;
  9. # 需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:
  10. location /admin/ {
  11. allow 192.168.200.32;
  12. allow 127.0.0.1;
  13. deny all;
  14. }
  15. }
  16. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  17. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  18. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  19. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload

image.png
image.png
1.2测试

  1. [root@lnmp ~]# echo "1234" > /data/nginx/test.com/admin/1.html
  2. # 测试 可以把配置文件改为192.168.200.1允许访问,使用浏览器测试
  3. [root@tomcat ~]# curl test.com/admin/1.html
  4. 1234
  5. [root@tomcat ~]# curl test.com/admin/1.html
  6. <html>
  7. <head><title>403 Forbidden</title></head>
  8. <body>
  9. <center><h1>403 Forbidden</h1></center>
  10. <hr><center>nginx/1.17.8</center>
  11. </body>
  12. </html>

image.png

九、nginx解析PHP

配置

  1. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. server
  3. {
  4. listen 80;
  5. server_name test.com test1.com test2.com;
  6. index index.html index.htm index.php;
  7. root /data/nginx/test.com;
  8. access_log /tmp/1.log combined_realip;
  9. location ~ \.php$ {
  10. include fastcgi_params;
  11. fastcgi_pass unix:/tmp/php-fcgi.sock;
  12. fastcgi_index index.php;
  13. fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_script_name; # fastcgi_pass 用来指定php-fpm监听的地址或者socket
  14. }
  15. }
  16. root@lnmp ~]# vim /data/nginx/test.com/3.php
  17. <?php
  18. phpinfo();
  19. ?>
  20. # fastcgi_pass用来指定php-fpm的地址 路径如果错误,则报错502
  21. # 路径在这个配置文件中
  22. [root@lnmp ~]# cat /usr/local/php-fpm/etc/php-fpm.conf
  23. [global]
  24. pid = /usr/local/php-fpm/var/run/php-fpm.pid
  25. error_log = /usr/local/php-fpm/var/log/php-fpm.log
  26. [zzx]
  27. listen = /tmp/php-fcgi.sock
  28. # listen = 127.0.0.1:9000 # 也可以这样配置,但是他们的配置文件要对应。
  29. listen.mode = 666
  30. user = php-fpm
  31. group = php-fpm
  32. pm = dynamic
  33. pm.max_children = 50
  34. pm.start_servers = 20
  35. pm.min_spare_servers = 5
  36. pm.max_spare_servers = 35
  37. pm.max_requests = 500
  38. rlimit_files = 1024
  39. # 注意一下这三行的配置文件与nginx配置文件的关系
  40. listen = /tmp/php-fcgi.sock
  41. # listen = 127.0.0.1:9000 # 也可以这样配置,但是他们的配置文件要对应。
  42. listen.mode = 666

image.png

测试

  1. [root@lnmp ~]# curl -x127.0.0.1:80 test.com/3.php
  2. <?php
  3. phpinfo();
  4. ?>
  5. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  6. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  7. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  8. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload

image.png
image.png

十、Nginx代理

一个没有公网IP的服务器要提供web服务,可以通过代理实现。

配置

  1. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/proxy.conf
  2. server
  3. {
  4. listen 80;
  5. server_name ask.apelearn.com;
  6. location /
  7. {
  8. proxy_pass http://47.104.7.242/;
  9. proxy_set_header Host $host;
  10. proxy_set_header X-Real-IP $remote_addr;
  11. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  12. }
  13. }

image.png

测试

  1. [root@lnmp ~]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
  2. <html>
  3. <head><title>404 Not Found</title></head>
  4. <body>
  5. <center><h1>404 Not Found</h1></center>
  6. <hr><center>nginx/1.17.8</center>
  7. </body>
  8. </html>
  9. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  10. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  11. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  12. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
  13. [root@lnmp ~]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
  14. #
  15. # robots.txt for MiWen
  16. #
  17. User-agent: *
  18. Disallow: /?/admin/
  19. Disallow: /?/people/
  20. Disallow: /?/question/
  21. Disallow: /account/
  22. Disallow: /app/
  23. Disallow: /cache/
  24. Disallow: /install/
  25. Disallow: /models/
  26. Disallow: /crond/run/
  27. Disallow: /search/
  28. Disallow: /static/
  29. Disallow: /setting/
  30. Disallow: /system/
  31. Disallow: /tmp/
  32. Disallow: /themes/
  33. Disallow: /uploads/
  34. Disallow: /url-*
  35. Disallow: /views/
  36. Disallow: /*/ajax/[root@lnmp ~]#

image.png

十一、负载均衡

一个IP叫做代理,两个以上就叫做负载均衡。

配置

  1. # 安装dig命令
  2. [root@lnmp ~]# yum install -y bind-utils
  3. # 通过dig命令获取相应域名的地址
  4. # 这里是拿百度的做测试
  5. [root@lnmp ~]# dig www.baidu.com
  6. ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.7 <<>> www.baidu.com
  7. ;; global options: +cmd
  8. ;; Got answer:
  9. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1532
  10. ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
  11. ;; OPT PSEUDOSECTION:
  12. ; EDNS: version: 0, flags:; udp: 512
  13. ;; QUESTION SECTION:
  14. ;www.baidu.com. IN A
  15. ;; ANSWER SECTION:
  16. www.baidu.com. 976 IN CNAME www.a.shifen.com.
  17. www.a.shifen.com. 192 IN A 110.242.68.4
  18. www.a.shifen.com. 192 IN A 110.242.68.3
  19. ;; Query time: 31 msec
  20. ;; SERVER: 114.114.114.114#53(114.114.114.114)
  21. ;; WHEN: 9 04 03:31:36 CST 2021
  22. ;; MSG SIZE rcvd: 101
  23. root@lnmp ~]# vim /usr/local/nginx/conf/vhost/load.conf
  24. upstream baidu
  25. {
  26. ip_hash;
  27. server 220.181.38.149:80;
  28. server 220.181.38.150:80;
  29. }
  30. server
  31. {
  32. listen 80;
  33. server_name www.baidu.com;
  34. location /
  35. {
  36. proxy_pass http://baidu;
  37. proxy_set_header Host $host;
  38. proxy_set_header X-Real-IP $remote_addr;
  39. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  40. }
  41. }
  42. # upstream来指定多个web server
  43. # upstream后面的名字要和proxy_pass后面的名字相同

image.png
image.png

测试

  1. [root@lnmp ~]# curl -x127.0.0.1:80 www.baidu.com
  2. default server!
  3. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -t
  4. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  5. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  6. [root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
  7. [root@lnmp ~]# curl -x127.0.0.1:80 www.baidu.com
  8. <!DOCTYPE html>
  9. <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
  10. # 百度一下,你就知道

image.png

十二、SSL

我们通常访问的网站有http和https 其中https就是和ssl证书有关。

SSL工作流程

image.png
image.png

image.png生成ssl密钥对

  1. [root@lnmp ~]# rpm -qa openssl
  2. openssl-1.0.2k-21.el7_9.x86_64
  3. [root@lnmp ~]# cd /usr/local/nginx/conf/
  4. # 配置密码必须是四位或者四位以上
  5. [root@lnmp conf]# openssl genrsa -des3 -out tmp.key 2048 # key文件为私钥
  6. Generating RSA private key, 2048 bit long modulus
  7. .........................................+++
  8. .................+++
  9. e is 65537 (0x10001)
  10. Enter pass phrase for tmp.key:
  11. Verifying - Enter pass phrase for tmp.key:
  12. [root@lnmp conf]# openssl rsa -in tmp.key -out aminglinux.key # 转换key,取消密码
  13. Enter pass phrase for tmp.key:
  14. writing RSA key
  15. [root@lnmp conf]# ls
  16. adminglinux.csr htpasswd test.csr
  17. adminglinux.key koi-utf test.key
  18. aminglinux.crt koi-win tmp.key
  19. aminglinux.csr mime.types uwsgi_params
  20. aminglinux.key mime.types.default uwsgi_params.default
  21. fastcgi.conf nginx.conf vhost
  22. fastcgi.conf.default nginx.conf.default win-utf
  23. fastcgi_params scgi_params
  24. fastcgi_params.default scgi_params.default
  25. [root@lnmp conf]# rm -rf tmp.key
  26. [root@lnmp conf]# openssl req -new -key aminglinux.key -out aminglinux.csr # 生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
  27. You are about to be asked to enter information that will be incorporated # 这里的aminglinux.crt为公钥
  28. into your certificate request.
  29. What you are about to enter is what is called a Distinguished Name or a DN.
  30. There are quite a few fields but you can leave some blank
  31. For some fields there will be a default value,
  32. If you enter '.', the field will be left blank.
  33. -----
  34. Country Name (2 letter code) [XX]:66
  35. State or Province Name (full name) []:beijing
  36. Locality Name (eg, city) [Default City]:beijing
  37. Organization Name (eg, company) [Default Company Ltd]:cn
  38. Organizational Unit Name (eg, section) []:cn
  39. Common Name (eg, your name or your server's hostname) []:cn
  40. Email Address []:cn@168.com
  41. Please enter the following 'extra' attributes
  42. to be sent with your certificate request
  43. A challenge password []:1234
  44. An optional company name []:1234
  45. [root@lnmp conf]# openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
  46. Signature ok
  47. subject=/C=66/ST=beijing/L=beijing/O=cn/OU=cn/CN=cn/emailAddress=cn@168.com
  48. Getting Private key

配置SSL

  1. root@lnmp conf]# vim /usr/local/nginx/conf/vhost/ssl.conf
  2. server
  3. {
  4. listen 443 ssl; # 低版本用listen 443;
  5. server_name 1234.com;
  6. index index.html index.php;
  7. root /data/nginx/1234.com;
  8. # 低版本这里需要添加ssl on;
  9. ssl_certificate aminglinux.crt;
  10. ssl_certificate_key aminglinux.key;
  11. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  12. }
  13. [root@lnmp conf]# /usr/local/nginx/sbin/nginx -t
  14. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  15. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  16. # 重新编译安装nginx
  17. [root@lnmp conf]# cd /usr/local/src/
  18. [root@lnmp src]# ls
  19. mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30
  20. nginx-1.17.8 php-5.6.30.tar.gz
  21. nginx-1.17.8.tar.gz
  22. [root@lnmp src]# cd nginx-1.17.8
  23. [root@lnmp nginx-1.17.8]# ls
  24. auto CHANGES.ru configure html Makefile objs src
  25. CHANGES conf contrib LICENSE man README
  26. [root@lnmp nginx-1.17.8]# ./configure --help |grep ssl
  27. --with-http_ssl_module enable ngx_http_ssl_module
  28. --with-mail_ssl_module enable ngx_mail_ssl_module
  29. --with-stream_ssl_module enable ngx_stream_ssl_module
  30. --with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
  31. --with-openssl=DIR set path to OpenSSL library sources
  32. --with-openssl-opt=OPTIONS set additional build options for OpenSSL
  33. [root@lnmp nginx-1.17.8]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  34. [root@lnmp nginx-1.17.8]# make && make install
  35. [root@lnmp nginx-1.17.8]# /etc/init.d/nginx restart
  36. Restarting nginx (via systemctl): [ 确定 ]
  37. [root@lnmp nginx-1.17.8]# netstat -ntlp
  38. Active Internet connections (only servers)
  39. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  40. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 56621/nginx: master
  41. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1097/sshd
  42. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1430/master
  43. tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 56621/nginx: master
  44. tcp6 0 0 :::3306 :::* LISTEN 19604/mysqld
  45. tcp6 0 0 :::22 :::* LISTEN 1097/sshd
  46. tcp6 0 0 ::1:25 :::* LISTEN 1430/master

image.png

image.png

image.png
在hosts
文件中加入 对应的域名 测试
image.png

任务5 php-fpm

php-fpm的pool

Nginx可以配置多个主机,php-fpm也可以配置多个pool
配置多个pool可以隔离开各个网站,例如一个网站有问题,把PHP资源耗尽了,那其他网站和这个网站使用的是同一个pool 那也会受到影响,所以我们可以给每个站点都配置一个pool。网站报错502一般和PHP有关。

给不同的站点分别配置不同的pool

  1. # 给两个站点分别配置不同的pool
  2. [root@lnmp nginx-1.17.8]# ps -ef |grep php
  3. root 36762 1 0 903 ? 00:00:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  4. php-fpm 36763 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  5. php-fpm 36764 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  6. php-fpm 36765 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  7. php-fpm 36766 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  8. php-fpm 36767 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  9. php-fpm 36768 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  10. php-fpm 36769 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  11. php-fpm 36770 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  12. php-fpm 36771 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  13. php-fpm 36772 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  14. php-fpm 36773 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  15. php-fpm 36774 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  16. php-fpm 36775 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  17. php-fpm 36776 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  18. php-fpm 36777 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  19. php-fpm 36778 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  20. php-fpm 36779 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  21. php-fpm 36780 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  22. php-fpm 36781 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  23. php-fpm 36782 36762 0 903 ? 00:00:00 php-fpm: pool zzx
  24. root 56629 39690 0 05:45 pts/2 00:00:00 grep --color=auto php
  25. # 进入配置文件目录
  26. [root@lnmp nginx-1.17.8]# cd /usr/local/php-fpm/etc/
  27. # 添加一个pool为123.com
  28. [root@lnmp etc]# vim php-fpm.conf
  29. [global]
  30. pid = /usr/local/php-fpm/var/run/php-fpm.pid
  31. error_log = /usr/local/php-fpm/var/log/php-fpm.log
  32. [zzx]
  33. listen = /tmp/php-fcgi.sock
  34. listen.mode = 666
  35. user = php-fpm
  36. group = php-fpm
  37. pm = dynamic
  38. pm.max_children = 50
  39. pm.start_servers = 20
  40. pm.min_spare_servers = 5
  41. pm.max_spare_servers = 35
  42. pm.max_requests = 500
  43. rlimit_files = 1024
  44. [123.com]
  45. listen = /tmp/123.sock
  46. listen.mode = 666
  47. user = php-fpm
  48. group = php-fpm
  49. pm = dynamic
  50. pm.max_children = 50
  51. pm.start_servers = 20
  52. pm.min_spare_servers = 5
  53. pm.max_spare_servers = 35
  54. pm.max_requests = 500
  55. rlimit_files = 1024
  56. # 检查语法
  57. [root@lnmp etc]# /usr/local/php-fpm/sbin/php-fpm -t
  58. [04-Sep-2021 05:49:14] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  59. # 重启
  60. [root@lnmp etc]# /etc/init.d/php-fpm restart
  61. Gracefully shutting down php-fpm . done
  62. Starting php-fpm done
  63. # 看到多了新的pool
  64. [root@lnmp etc]# ps -aux |grep php
  65. root 56639 0.2 0.5 231776 5104 ? Ss 05:49 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  66. php-fpm 56640 0.0 0.4 231716 4828 ? S 05:49 0:00 php-fpm: pool zzx
  67. php-fpm 56641 0.0 0.4 231716 4828 ? S 05:49 0:00 php-fpm: pool zzx
  68. php-fpm 56642 0.0 0.4 231716 4828 ? S 05:49 0:00 php-fpm: pool zzx
  69. php-fpm 56643 0.0 0.4 231716 4828 ? S 05:49 0:00 php-fpm: pool zzx
  70. php-fpm 56644 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  71. php-fpm 56645 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  72. php-fpm 56646 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  73. php-fpm 56647 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  74. php-fpm 56648 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  75. php-fpm 56649 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  76. php-fpm 56650 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool zzx
  77. php-fpm 56651 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  78. php-fpm 56652 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  79. php-fpm 56653 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  80. php-fpm 56654 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  81. php-fpm 56655 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  82. php-fpm 56656 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  83. php-fpm 56657 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  84. php-fpm 56658 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  85. php-fpm 56659 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool zzx
  86. php-fpm 56660 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool 123.com
  87. php-fpm 56661 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool 123.com
  88. php-fpm 56662 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool 123.com
  89. php-fpm 56663 0.0 0.4 231716 4832 ? S 05:49 0:00 php-fpm: pool 123.com
  90. php-fpm 56664 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool 123.com
  91. php-fpm 56665 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool 123.com
  92. php-fpm 56666 0.0 0.4 231716 4836 ? S 05:49 0:00 php-fpm: pool 123.com
  93. php-fpm 56667 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  94. php-fpm 56668 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  95. php-fpm 56669 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  96. php-fpm 56670 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  97. php-fpm 56671 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  98. php-fpm 56672 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  99. php-fpm 56673 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  100. php-fpm 56674 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  101. php-fpm 56675 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  102. php-fpm 56676 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  103. php-fpm 56677 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  104. php-fpm 56678 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  105. php-fpm 56679 0.0 0.4 231716 4840 ? S 05:49 0:00 php-fpm: pool 123.com
  106. root 56682 0.0 0.0 112660 964 pts/2 R+ 05:50 0:00 grep --color=auto php
  107. [root@lnmp vhost]# vim default.conf
  108. server
  109. {
  110. listen 80 default_server;
  111. server_name aaa.com;
  112. index index.html index.htm index.php;
  113. root /data/nginx/default;
  114. location ~ \.php$ {
  115. include fastcgi_params;
  116. fastcgi_pass unix:/tmp/123.sock;
  117. fastcgi_index index.php;
  118. fastcgi_param SCRIPT_FILENAME /data/nginx/test.com$fastcgi_scr
  119. ipt_name;
  120. }
  121. }
  122. # fastcgi_pass unix:/tmp/123.sock; 主要就是修改这一行配置文件选择不同的pool。
  123. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -t
  124. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  125. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  126. [root@lnmp vhost]# /usr/local/nginx/sbin/nginx -s reload

image.png

image.png

image.png

image.png

配置拆分多个pool

在nginx.conf这个配置文件下有一行配置文件为 include vhost/*.conf; 他的作用为识别指定目录下以conf为结尾的配置文件,让每个站点为一个配置文件。

  1. # 进入配置文件目录
  2. root@lnmp vhost]# cd /usr/local/php-fpm/etc/
  3. # 加入一行include
  4. [root@lnmp etc]# vim php-fpm.conf
  5. [global]
  6. pid = /usr/local/php-fpm/var/run/php-fpm.pid
  7. error_log = /usr/local/php-fpm/var/log/php-fpm.log
  8. include = etc/php-fpm.d/*.conf
  9. [zzx]
  10. listen = /tmp/php-fcgi.sock
  11. listen.mode = 666
  12. user = php-fpm
  13. group = php-fpm
  14. pm = dynamic
  15. pm.max_children = 50
  16. pm.start_servers = 20
  17. pm.min_spare_servers = 5
  18. pm.max_spare_servers = 35
  19. pm.max_requests = 500
  20. rlimit_files = 1024
  21. [123.com]
  22. listen = /tmp/123.sock
  23. listen.mode = 666
  24. user = php-fpm
  25. group = php-fpm
  26. pm = dynamic
  27. pm.max_children = 50
  28. pm.start_servers = 20
  29. pm.min_spare_servers = 5
  30. pm.max_spare_servers = 35
  31. pm.max_requests = 500
  32. rlimit_files = 1024
  33. # 创建存放配置文件的目录
  34. ▽root@lnmp etc]# mkdir php-fpm.d
  35. [root@lnmp etc]# cd php-fpm.d/
  36. [root@lnmp php-fpm.d]# vim 123.conf
  37. [123.com]
  38. listen = /tmp/123.sock
  39. listen.mode = 666
  40. user = php-fpm
  41. group = php-fpm
  42. pm = dynamic
  43. pm.max_children = 50
  44. pm.start_servers = 20
  45. pm.min_spare_servers = 5
  46. pm.max_spare_servers = 35
  47. pm.max_requests = 500
  48. rlimit_files = 1024
  49. [root@lnmp php-fpm.d]# vim zzx.conf
  50. [zzx]
  51. listen = /tmp/php-fcgi.sock
  52. listen.mode = 666
  53. user = php-fpm
  54. group = php-fpm
  55. pm = dynamic
  56. pm.max_children = 50
  57. pm.start_servers = 20
  58. pm.min_spare_servers = 5
  59. pm.max_spare_servers = 35
  60. pm.max_requests = 500
  61. rlimit_files = 1024
  62. [root@lnmp php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
  63. [04-Sep-2021 06:39:50] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  64. # 重启服务
  65. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
  66. Gracefully shutting down php-fpm . done
  67. Starting php-fpm done
  68. # 查看一下sock文件
  69. [root@lnmp php-fpm.d]# ls /tmp/*.sock
  70. /tmp/123.sock /tmp/mysql.sock /tmp/php-fcgi.sock
  71. [root@lnmp php-fpm.d]# ps -ef |grep php
  72. root 56736 1 0 06:40 ? 00:00:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
  73. php-fpm 56737 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  74. php-fpm 56738 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  75. php-fpm 56739 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  76. php-fpm 56740 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  77. php-fpm 56741 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  78. php-fpm 56742 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  79. php-fpm 56743 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  80. php-fpm 56744 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  81. php-fpm 56745 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  82. php-fpm 56746 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  83. php-fpm 56747 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  84. php-fpm 56748 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  85. php-fpm 56749 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  86. php-fpm 56750 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  87. php-fpm 56751 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  88. php-fpm 56752 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  89. php-fpm 56753 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  90. php-fpm 56754 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  91. php-fpm 56755 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  92. php-fpm 56756 56736 0 06:40 ? 00:00:00 php-fpm: pool 123.com
  93. php-fpm 56757 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  94. php-fpm 56758 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  95. php-fpm 56759 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  96. php-fpm 56760 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  97. php-fpm 56761 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  98. php-fpm 56762 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  99. php-fpm 56763 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  100. php-fpm 56764 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  101. php-fpm 56765 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  102. php-fpm 56766 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  103. php-fpm 56767 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  104. php-fpm 56768 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  105. php-fpm 56769 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  106. php-fpm 56770 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  107. php-fpm 56771 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  108. php-fpm 56772 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  109. php-fpm 56773 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  110. php-fpm 56774 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  111. php-fpm 56775 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  112. php-fpm 56776 56736 0 06:40 ? 00:00:00 php-fpm: pool zzx
  113. root 56780 39690 0 06:41 pts/2 00:00:00 grep --color=auto php

image.png

image.png
image.png

image.png

php-fpm查看慢执行日志

通过配置慢执行日志,可以让我们清晰的了解到PHP脚本在哪里执行时间长,可以定位到哪一行。

  1. # 添加两行配置文件
  2. [root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/zzx.conf
  3. request_slowlog_timeout = 1
  4. slowlog = /usr/local/php-fpm/var/log/zzx-slow.log
  5. [root@lnmp php-fpm.d]# /usr/local/php-fpm/sbin/php-fpm -t
  6. [04-Sep-2021 06:48:16] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
  7. # 重载PHP服务
  8. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm reload
  9. Reload service php-fpm done
  10. # 可以看到我们配置的路径已经多了一个
  11. [root@lnmp php-fpm.d]# ls /usr/local/php-fpm/var/log/
  12. php-fpm.log zzx-slow.log
  13. # 现在的www-slow.log还是一个空文件。只会记录执行时间大于1s的日志
  14. [root@lnmp php-fpm.d]# cat /usr/local/php-fpm/var/log/zzx-slow.log
  15. # 测试 写一个慢执行文件
  16. root@lnmp php-fpm.d]# vim /data/nginx/test.com/sleep.php
  17. <?php
  18. echo "test slow log";
  19. sleep(2);
  20. echo "done";
  21. ?>
  22. # 执行这个脚本的时候可以明显看到慢了一下。
  23. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
  24. test slow logdone[root@lnmp php-fpm.d]#
  25. # 可以看到日志多出一行。说哪个文件,第三行 sellp()问题。
  26. [root@lnmp php-fpm.d]# cat /usr/local/php-fpm/var/log/zzx-slow.log
  27. [04-Sep-2021 06:54:05] [pool zzx] pid 56827
  28. script_filename = /data/nginx/test.com/sleep.php
  29. [0x00007fd1f18cf288] sleep() /data/nginx/test.com/sleep.php:3

image.png

php-fpm定义open_basedir

open_basedir目的就是安全,httpd可以针对每个虚拟机设置一个open_basedir
php-fpm同样也可以对不同的pool设置的不同的open_basedir

  1. # 修改配置文件,特意配置为错误路径
  2. # 添加一行配置文件,其中test.com的路径为错误路径
  3. [root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/zzx.conf
  4. php_admin_value[open_basedir]=/data/nginx/zzxtest.com:/tmp/
  5. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
  6. Gracefully shutting down php-fpm . done
  7. Starting php-fpm done
  8. # 测试发现这些PHP文件都无法访问,就是因为php open_basedir做了限制。
  9. [root@lnmp php-fpm.d]# ls /data/nginx/test.com/
  10. 1.js 1.jss 2.jpg 3.php admin index.html sleep.php
  11. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
  12. HTTP/1.1 404 Not Found
  13. Server: nginx/1.17.8
  14. Date: Fri, 03 Sep 2021 23:02:37 GMT
  15. Content-Type: text/html; charset=UTF-8
  16. Connection: keep-alive
  17. X-Powered-By: PHP/5.6.30
  18. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
  19. HTTP/1.1 404 Not Found
  20. Server: nginx/1.17.8
  21. Date: Fri, 03 Sep 2021 23:02:48 GMT
  22. Content-Type: text/html; charset=UTF-8
  23. Connection: keep-alive
  24. X-Powered-By: PHP/5.6.30
  25. # 把配置文件改为正确的
  26. # 发现可以正常使用
  27. root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/zzx.conf
  28. php_admin_value[open_basedir]=/data/nginx/test.com:/tmp/
  29. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
  30. Gracefully shutting down php-fpm . done
  31. Starting php-fpm done
  32. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
  33. HTTP/1.1 200 OK
  34. Server: nginx/1.17.8
  35. Date: Fri, 03 Sep 2021 23:05:20 GMT
  36. Content-Type: text/html; charset=UTF-8
  37. Connection: keep-alive
  38. X-Powered-By: PHP/5.6.30
  39. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
  40. HTTP/1.1 200 OK
  41. Server: nginx/1.17.8
  42. Date: Fri, 03 Sep 2021 23:05:27 GMT
  43. Content-Type: text/html; charset=UTF-8
  44. Connection: keep-alive
  45. X-Powered-By: PHP/5.6.30

image.png
image.png

配置错误日志

配置记录php执行错误的日志

  1. # 配置
  2. [root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php.ini
  3. display_errors = Off
  4. log_errors = On
  5. error_log = /usr/local/php-fpm/var/log/php_error.log
  6. ;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
  7. error_reporting = E_ALL
  8. # 再次将php的配置文件改错
  9. [root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/zzx.conf
  10. php_admin_value[open_basedir]=/data/nginx/zzxtest.com:/tmp/
  11. # 这个日志文件不会自动生成,所以我们自己创建一个,并授权。
  12. [root@lnmp php-fpm.d]# touch /usr/local/php-fpm/var/log/php_error.log
  13. [root@lnmp php-fpm.d]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
  14. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
  15. Gracefully shutting down php-fpm . done
  16. Starting php-fpm done
  17. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
  18. HTTP/1.1 404 Not Found
  19. Server: nginx/1.17.8
  20. Date: Fri, 03 Sep 2021 23:26:40 GMT
  21. Content-Type: text/html; charset=UTF-8
  22. Connection: keep-alive
  23. X-Powered-By: PHP/5.6.30
  24. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
  25. HTTP/1.1 404 Not Found
  26. Server: nginx/1.17.8
  27. Date: Fri, 03 Sep 2021 23:28:18 GMT
  28. Content-Type: text/html; charset=UTF-8
  29. Connection: keep-alive
  30. X-Powered-By: PHP/5.6.30
  31. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php
  32. No input file specified.
  33. # 查看错误日志,显示open_basedir限制生效,目录不能访问
  34. [root@lnmp php-fpm.d]# cat /usr/local/php-fpm/var/log/php_error.log
  35. [03-Sep-2021 23:26:40 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/3.php) is not within the allowed path(s): (/data/nginx/zzxtest.com:/tmp/) in Unknown on line 0
  36. [03-Sep-2021 23:26:40 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  37. [03-Sep-2021 23:27:43 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/zzxtest.com:/tmp/) in Unknown on line 0
  38. [03-Sep-2021 23:27:43 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  39. [03-Sep-2021 23:27:55 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/zzxtest.com:/tmp/) in Unknown on line 0
  40. [03-Sep-2021 23:27:55 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  41. [03-Sep-2021 23:28:18 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/zzxtest.com:/tmp/) in Unknown on line 0
  42. [03-Sep-2021 23:28:18 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  43. [03-Sep-2021 23:28:41 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/nginx/test.com/sleep.php) is not within the allowed path(s): (/data/nginx/zzxtest.com:/tmp/) in Unknown on line 0
  44. [03-Sep-2021 23:28:41 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
  45. # 再次将配置文件修改回来
  46. root@lnmp php-fpm.d]# vim /usr/local/php-fpm/etc/php-fpm.d/zzx.conf
  47. [zzx]
  48. listen = /tmp/php-fcgi.sock
  49. listen.mode = 666
  50. user = php-fpm
  51. group = php-fpm
  52. pm = dynamic
  53. pm.max_children = 50
  54. pm.start_servers = 20
  55. pm.min_spare_servers = 5
  56. pm.max_spare_servers = 35
  57. pm.max_requests = 500
  58. rlimit_files = 1024
  59. request_slowlog_timeout = 1
  60. slowlog = /usr/local/php-fpm/var/log/zzx-slow.log
  61. php_admin_value[open_basedir]=/data/nginx/test.com:/tmp/
  62. # 重启服务,可以正常使用,也没有产生错误日志。
  63. [root@lnmp php-fpm.d]# /etc/init.d/php-fpm restart
  64. Gracefully shutting down php-fpm . done
  65. Starting php-fpm done
  66. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/sleep.php -I
  67. HTTP/1.1 200 OK
  68. Server: nginx/1.17.8
  69. Date: Fri, 03 Sep 2021 23:32:04 GMT
  70. Content-Type: text/html; charset=UTF-8
  71. Connection: keep-alive
  72. X-Powered-By: PHP/5.6.30
  73. [root@lnmp php-fpm.d]# curl -x127.0.0.1:80 test.com/3.php -I
  74. HTTP/1.1 200 OK
  75. Server: nginx/1.17.8
  76. Date: Fri, 03 Sep 2021 23:32:09 GMT
  77. Content-Type: text/html; charset=UTF-8
  78. Connection: keep-alive
  79. X-Powered-By: PHP/5.6.30

image.png
image.png
image.png
image.png

php-fpm进程管理

pm = dynamic //动态进程管理,也可以是static 如果是静态的,会直接启动最大子进程数
pm.max_children = 50 //最大子进程数,ps aux可以查看
pm.start_servers = 20 //启动服务时会启动的进程数
pm.min_spare_servers = 5 //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。
pm.max_spare_servers = 35 //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。
pm.max_requests = 500 //定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。
image.png
Shell
复制代码


Shell
复制代码


Shell

复制代码