• ubuntu22.4配置nginx和php


    实验操作步骤

    1. 安装ngix
      这里使用命令:
      sudo apt install nginx
    2.	icestone@icestone-nb:~$ sudo apt install nginx
    3.	[sudo] icestone 的密码: 
    4.	正在读取软件包列表... 完成
    5.	正在分析软件包的依赖关系树... 完成
    6.	正在读取状态信息... 完成                 
    7.	将会同时安装下列软件:
    8.	  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libnginx-mod-stream-geoip2 nginx-common nginx-core
    9.	下列【新】软件包将被安装:
    10.	  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libnginx-mod-stream-geoip2 nginx nginx-common nginx-core
    11.	升级了 0 个软件包,新安装了 9 个软件包,要卸载 0 个软件包,有 2 个软件包未被升级。
    12.	需要下载 696 kB 的归档。
    13.	解压缩后会消耗 2,395 kB 的额外空间。
    14.	您希望继续执行吗? [Y/n] Y
    2.调整防火墙:
    sudo ufw app list
    15.	icestone@icestone-nb:~$ sudo ufw allow 'Nginx HTTP'
    16.	规则已添加
    17.	规则已添加 (v6)
    18.	icestone@icestone-nb:~$ sudo ufw allow 'Nginx HTTPS'
    19.	规则已添加
    20.	规则已添加 (v6)
    21.	icestone@icestone-nb:~$ sudo ufw allow 'Nginx Full'
    22.	规则已添加
    23.	规则已添加 (v6)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    查看结果:
    sudo ufw status

    24.	icestone@icestone-nb:~$ sudo ufw status
    25.	状态: 激活
    26.	
    27.	至                          动作          来自
    28.	-                          --          --
    29.	                 ALLOW       Anywhere                  
    30.	80/tcp                     ALLOW       Anywhere                  
    31.	443/tcp                    ALLOW       Anywhere
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    success

    3.检查web服务:
    systemctl status nginx

    32.	icestone@icestone-nb:~$ systemctl status nginx
    33.	● nginx.service - A high performance web server and a reverse proxy server
    34.	     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    35.	     Active: active (running) since Thu 2022-11-10 23:07:21 CST; 1h 14min ago
    36.	       Docs: man:nginx(8)
    37.	    Process: 21371 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    38.	    Process: 21374 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    success
    39. 访问;
    http://localhost/
    在这里插入图片描述

    2.开启https:
    40. 修改配置文件
    这里由于使用的ubuntu22.04
    修改 /etc/nginx/sites-available/default
    添加:

    41.	# 下面是我自己写的配置:
    42.	server{
    43.	 listen 443 ssl;
    44.	 listen [::]:443 ssl;
    45.	 root /app/;
    46.	 index index.html;
    47.	 server_name laotie666.xyz www.laotie666.xyz;
    48.	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.申请一个自签证书:

    49.	icestone@icestone-nb:/app$ openssl req -new -x509 -nodes -out server.crt -keyout server.key
    50.	...+.........+...+......+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.+..+.+...+..+...+.........+...+..................+....+......+..+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*.....+.....+...+....+..+...+...................
    
    • 1
    • 2

    查看当前目录:

    51.	icestone@icestone-nb:/app$ ll
    52.	总用量 20
    53.	drwxrwxrwx  3 root     root     4096 1111 02:29 ./
    54.	drwxr-xr-x 25 root     root     4096 1111 00:39 ../
    55.	-rw-------  1 icestone icestone    0 1111 02:27 ca.key
    56.	lrwxrwxrwx  1 icestone icestone   20 1111 02:26 openssl.cnf -> /etc/ssl/openssl.cnf*
    57.	-rw-rw-r--  1 icestone icestone 1363 1111 02:29 server.crt
    58.	-rw-------  1 icestone icestone 1704 1111 02:29 server.key
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    crt和key文件生成了,将其配置在nginx的配置中:
    修改/etc/nginx/sites-available/default:
    在与原来server上修改

    59.	 sserver {
    60.	 listen       443 ssl;
    61.	 index index.html index.htm index.nginx-debian.html;
    62.	
    63.	 server_name icestone.art www.icestone.art;
    64.	
    65.	 location ~ \.php$ {
    66.	   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    67.	   fastcgi_pass unix:/run/php-fpm/www.sock;
    68.	   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    69.	   fastcgi_index index.php;
    70.	   include fastcgi_params;
    71.	 }
    72.	 ssl_certificate      /app/server.crt;
    73.	 ssl_certificate_key  /app/server.key;
    74.	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    这里icestone.art使我们即将使用的域名,域名在、etc/hosts下直接添加并刷新即可,
    要注意Linux下回环地址是0.0.0.0
    75. https访问:
    在这里插入图片描述

    点查看证书:
    在这里插入图片描述
    强制访问:
    在这里插入图片描述
    76. 安装php
    sudo apt install php libapache2-mod-php

    77.	icestone@icestone-nb:/app$ sudo apt install php-fpm
    78.	正在读取软件包列表... 完成
    79.	正在分析软件包的依赖关系树... 完成
    80.	正在读取状态信息... 完成                 
    81.	将会同时安装下列软件:
    82.	  php8.1-fpm
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意这里显示安装的是php8.1-fpm,运行检查:
    systemctl status php8.1-fpm

    83.	icestone@icestone-nb:/app$ systemctl status php8.1-fpm
    84.	● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
    85.	     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
    
    • 1
    • 2
    • 3

    success
    配置php运行端口和允许端口:
    修改 /etc/php/8.1/pool.d/www.conf
    修改下面三个:

    86.	;下面这个注释掉
    87.	;listen = /run/php/php8.1-fpm.sock
    88.	;修改:
    89.	listen = localhost:81
    90.	listen.allowed_clients = 127.0.0.1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    修改nginx中监听php的端口;
    修改 /etc/nginx/sites-available/default
    主要修改80端口和443端口下启动页面,监听php的端口:

    91.	server {
    92.	 listen 80 default_server;
    93.	 listen [::]:80 default_server;
    94.	 index index.php index.html index.htm index.nginx-debian.html;
    95.	 root /app/www/;
    96.	 server_name icestone.art www.icestone.art _;
    97.	 location / {
    98.	   try_files $uri $uri/ /index.php$is_args$args;
    99.	 }
    100.	 location ~ \.php$ {
    101.	   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    102.	   fastcgi_pass localhost:81;
    103.	         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    104.	   fastcgi_index index.php;
    105.	   include fastcgi_params;
    106.	 }
    107.	}
    108.	
    109.	server {
    110.	 listen 443 ssl;
    111.	 index index.php index.html index.htm index.nginx-debian.html;
    112.	 root /app/www/;
    113.	 server_name icestone.art www.icestone.art;
    114.	 location / {
    115.	   try_files $uri $uri/ /index.php$is_args$args;
    116.	 }
    117.	 location ~ \.php$ {
    118.	   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    119.	   fastcgi_pass localhost:81;
    120.	         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    121.	   fastcgi_index index.php;
    122.	   include fastcgi_params;
    123.	 }
    124.	 ssl_certificate      /app/server.crt;
    125.	 ssl_certificate_key  /app/server.key;
    126.	}
    127.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    nginx和php重启,访问浏览器端:
    在这里插入图片描述

  • 相关阅读:
    Cy3 diacid/bis-carboxylic acid,Cy3-COOH/carboxylic acid/羧基羧酸(bis)
    跨平台VSCODE教程
    spring 框架理论
    Java基础面试题突击系列5
    老叶的三束玫瑰
    计算机组成原理习题help
    光致发光谱荧光量子效率测量系统
    双目深度算法——双目深度算法总结
    SpringCloud系列(二)Ribbon 负载均衡的原理及详细流程
    STM32(垃圾桶开关盖)
  • 原文地址:https://blog.csdn.net/ice_stone_kai/article/details/127838484