nginx自动部署-跨操作系统

发布于:2024-05-10 ⋅ 阅读:(32) ⋅ 点赞:(0)

项目里面有一个需求,就是需要用让nginx进程提供给系统管理一个start,stop和getPid方法,这样系统管理可以自动拉起来nginx,达到自动部署的目的。离线部署同样适用

这样一来,我就需要提供windows版本linux不同版本的nginx源码包,windows版本的好办,就直接从官网下载的,我下载的是nginx1.24.0的稳定版本的,linux版本的就需要自己编译源码包了,因为wget下载的nginx首先产物目录就很不一样,我需要就是跟windows差不多的目录结构,便于我写nginx.conf配置文件

window的产物目录如下

linux产物目录如下

linux的这个产物只需要编译一次,后面部署就直接拷贝这个就可以了,编译的过程如下:

官网下载nginx源码,新建一个文件夹叫Nginx

进入到源码目录 

./configure --prefix=新建的产物目录地址
make -j12
make install 

可能中间需要装 pcre库,wget 下载一下好了。

pcre是因为我的nginx配置需要用到正则,就是html下面有不同文件夹,每个文件夹是一个项目,部署的时候有多少个文件夹部署多少个项目。

这个过程比较曲折,首先nginx没有地方写日志是不会启动成功的,所以配置文件需要指定日志的生成位置,还有配置文件的启动位置,pid等

贴一份windows的配置文件:

worker_processes  1;
error_log  D:/code/system/run/nginx/logs/error.log error;
pid        D:/code/system/run/nginx/logs/nginx.pid;
events { 
   worker_connections  1024; 
} 
http { 
   include       mime.types; 
   default_type  application/octet-stream; 
   sendfile        on; 
   keepalive_timeout  65; 
   access_log D:/code/system/run/nginx/logs/access.log; 
   map $_FilePath $_FilePathForUse { 
       "" "index.html"; 
       "/" "index.html";
       default $filePathFromRemote; 
   }
   	client_body_temp_path D:/code/system/run/nginx/temp/client_temp; 
	proxy_temp_path D:/code/system/run/nginx/temp/proxy_temp; 
	fastcgi_temp_path D:/code/system/run/nginx/temp/fastcgi_temp; 
	uwsgi_temp_path D:/code/system/run/nginx/temp/uwsgi_temp;
	scgi_temp_path D:/code/system/run/nginx/temp/scgi_temp; 
   server { 
       listen 8090; 
	
       location /{ 
           gzip on; 
           gzip_static on; 
           root  D:/code/system/run/nginx/html/; 
           add_header X-URI-Part1 $1; 
           add_header X-URI-Part2 $2; 
           add_header 'Access-Control-Allow-Origin' '*'; 
           add_header 'Access-Control-Allow-Credentials' 'true'; 
           set $module $1; 
           set $filePathFromRemote $2; 
           set $_FilePath "$filePathFromRemote"; 
       } 
       error_page 500 502 503 504 /50x.html; 
   } 
   server { 
        listen 9000; 
		location / {  
			root  D:/code/system/run; 
			autoindex on;  
			add_header Access-Control-Allow-Origin *; 
			add_header Access-Control-Allow-Headers X-Requested-With;  
			add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;  
		}  
    } 
} 

两个server是因为还用到了nginx做文件服务

linux版本的配置文件:

  worker_processes  1;
  
  error_log  ${ROOT_DIR}/run/nginx/logs/error.log error;
  pid         ${ROOT_DIR}/run/nginx/logs/nginx.pid;
  events {
      worker_connections  1024;
  }
  
  
  http {
      include       mime.types;
      default_type  application/octet-stream;
  
      sendfile        on;
      keepalive_timeout  65;
      access_log  ${ROOT_DIR}/run/nginx/logs/access.log; 
  
      map $_FilePath $_FilePathForUse {
          "" "index.html";
          "/" "index.html";
          default $filePathFromRemote;
      }
	  client_body_temp_path ${ROOT_DIR}/run/nginx/client_temp;
	  proxy_temp_path ${ROOT_DIR}/run/nginx/proxy_temp;
	  fastcgi_temp_path ${ROOT_DIR}/run/nginx/fastcgi_temp;
	  uwsgi_temp_path ${ROOT_DIR}/run/nginx/uwsgi_temp;
	  scgi_temp_path ${ROOT_DIR}/run/nginx/scgi_temp;
  
      server {
          listen 8090;
          server_name localhost;

          location ~ ^/([^/]+)(/?.*)$ {
              root  ${ROOT_DIR}/run/nginx/html/;
  
              add_header 'Access-Control-Allow-Origin' '*';
              add_header 'Access-Control-Allow-Credentials' 'true';
  
              set $module $1;
              set $filePathFromRemote $2;
              set $_FilePath "$filePathFromRemote";
          }
  
          error_page 500 502 503 504 /50x.html;
          location = /50x.html {
              root ${ROOT_DIR}/run/nginx/html;
          }
      }
	   server {
          listen 9000;
          server_name localhost;
          location / {
            root ${ROOT_DIR}/run;
            autoindex on;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
          }
      }
  }

这两个配置文件的生成是通过脚本自动写入的,用到了各自系统的环境变量

下面就是启动程序了

start方法


    #ifdef _WIN64
    cmd = fmt::format("{}/nginx/nginx -c {}/nginx/conf/nginx.conf -e {}/nginx/logs/error.log",
                      根目录,
                      根目录);
    #endif 

    #ifdef __linux__
    cmd = fmt::format("{}/nginx/sbin/nginx -c {}/nginx/conf/nginx.conf -e {}/nginx/logs/error.log",根目录,根目录,根目录);
    #endif

大概就是通过命令行启动nginx进程,涉及到一些公司里面封装的底层方法,就不方便写了。。

stop方法同理

getPid方法比较复杂,nginx每次启动成功都会往nginx.pid文件写入一个进程号,需要把文件读出来然后返回出去,而且需要自己判断进程是不是存活,不能返回一个没有启动的进程出去。

启动成功之后浏览器输入当前机器的ip地址:8090就能访问到内容

系统管理界面可以管理nginx的启停


网站公告

今日签到

点亮在社区的每一天
去签到