一、在 Ubuntu 系统中,可以通过以下命令安装 httpd-tools 工具包,为nginx配置密码:

  • 安装工具包

      apt-get update
      apt-get install apache2-utils
  • 创建用户和密码:

      htpasswd -c /etc/nginx/.htpasswd gz
  • 修改用户密码:

      /etc/nginx/.htpasswd gz

    二、安装typecho

https://docs.typecho.org/install
  • 下载php及相关依赖

    • 安装php

      sudo apt-get install php7.4 php7.4-sqlite php7.4-fpm php-mbstring php7.4-gd  
    • 安装依赖

      sudo apt install php php-cli php-fpm php-mysql php-json php-opcache php-mbstring php-xml  
  • 启动php服务:查找对应的 PHP-FPM systemd 服务单元文件。在 /lib/systemd/system/ 目录下查找以 php{version}-fpm.service 命名的文件,例如:

        service php7.4-fpm start
  • 下载源码(可以在typecho官网找到)

         wget https://github.com/typecho/typecho/releases/latest/download/typecho.zip

    解压源码

      unzip typecho.zip

    对整个源码目录赋予足够的权限

      chmod -R 777 /home/typecho
  • 配置nginx

      server{
          listen 80;
          server_name ****.xyz;
      
          root /var/www/typecho;
          index index.php;
      
          if (!-e $request_filename) {
              rewrite ^(.*)$ /index.php$1 last; 
          }
      
          location / {
              root html;
              index index.php index.html index.htm;
      
              set $path_info "";
              set $real_script_name $fastcgi_script_name;
              if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                      set $real_script_name $1;
                  set $path_info $2;
              }
              fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
              fastcgi_param SCRIPT_NAME $real_script_name;
              fastcgi_param PATH_INFO $path_info;
          }
          
          location /admin {
              index index.php;
              try_files $uri $uri/ /admin/index.php?$args;
          }
      
          location ~ .*\.php(\/.*)*$ {
                  include fastcgi.conf;
                  fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
          }
         
          # Typecho static files
          location ~* ^/(usr|admin|themes|plugins)/.*\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
              expires 24h;
              add_header Cache-Control public;
              access_log off;
      
              try_files $uri $uri/ /index.php?$args;
          }
      
      }
  • 访问:http://**.xyz/install.php 选择sqlite数据库然后下一步...
  • 配置nginx支持https:

      server{
              listen 443 ssl http2;
              #listen [::]:443 ssl http2;
              server_name ****.xyz www.****.xyz;
              ssl_certificate /root/.acme.sh/www.****.xyz_ecc/www.****.xyz.cer;
              ssl_certificate_key /root/.acme.sh/www.****.xyz_ecc/www.****.xyz.key;
              ssl_session_timeout 5m;
              ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
              ssl_prefer_server_ciphers on;
              ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
              ssl_session_cache builtin:1000 shared:SSL:10m;
      
              # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
              # ssl_dhparam /etc/nginx/dhparam.pem;
      
              add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
              add_header X-Frame-Options DENY;
              add_header X-Content-Type-Options nosniff;
      
              root /var/www/typecho;
              index index.php;
      
              if (!-e $request_filename) {
                  rewrite ^(.*)$ /index.php$1 last; 
              }
      
              location / {
                  root html;
                  index index.php index.html index.htm;
      
                  set $path_info "";
                  set $real_script_name $fastcgi_script_name;
                  if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                      set $real_script_name $1;
                      set $path_info $2;
                  }
                  fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                  fastcgi_param SCRIPT_NAME $real_script_name;
                  fastcgi_param PATH_INFO $path_info;
              } 
              location /admin {
                  index index.php;
                  try_files $uri $uri/ /admin/index.php?$args;
              }
              location ~ .*\.php(\/.*)*$ {
                      include fastcgi.conf;
                      fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
              }
              # Typecho static files
              location ~* ^/(usr|admin|themes|plugins)/.*\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
                  expires 24h;
                  add_header Cache-Control public;
                  access_log off;
      
                  try_files $uri $uri/ /index.php?$args;
              }
          }

注意事项:

配置nginx过程中注意php7.4-fpm.sock路径,即nginx配置文件中:

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

需要对应安装php-fpm对应的sock位置,否则会报错:

2023/07/04 10:47:59 [crit] 706081#706081: *29 connect() to unix:/var/run/php-fpm/www.sock failed (2: No such file or directory) while connecting to upstream, client: 111.198.117.47, server: 8.217.154.143, request: "GET /install.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/www.sock:", host: "8.217.154.143"