翼度科技»论坛 云主机 LINUX 查看内容

nginx编译安装以及常用参数详解

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
1 基于ansible role实现编译安装nginx

利用ansible控制端10.0.0.8机器,在被控制端10.0.0.18上部署nginx
首先打通ansible控制端与被控制端的基于key验证
  1. [root@ansible-rocky ~]$ ssh-copy-id 10.0.0.18
  2. [root@ansible-rocky ~]$ ssh 10.0.0.18
  3. Last login: Wed Jan 11 12:18:28 2023 from 10.0.0.8
  4. [root@rocky8 ~]$ hostname -I
  5. 10.0.0.18
复制代码
然后创建nginx项目目录实现基于role的部署任务
  1. #nginx role项目目录总览
  2. [root@ansible-rocky opt]$ tree /opt
  3. /opt
  4. ├── hosts_nginx
  5. ├── nginx_role.yml
  6. └── roles
  7.     └── nginx
  8.        ├── handlers
  9.        │   └── main.yml
  10.        ├── tasks
  11.        │   └── main.yml
  12.        └── templates
  13.            ├── nginx.conf.j2
  14.            └── nginx.service.j2
  15. #task文件
  16. [root@ansible-rocky roles]$ cat nginx/tasks/main.yml
  17. - name: add group nginx
  18.   group: name=nginx system=yes gid=80
  19. - name: add user nginx
  20.   user: name=nginx group=nginx uid=80 system=yes shell="/sbin/nologin" create_home=no
  21. - name: install dependent package
  22.   yum: name={{item}} state=latest
  23.   loop:
  24.     - gcc
  25.     - make
  26.     - pcre-devel
  27.     - openssl-devel
  28.     - zlib-devel
  29.     - perl-ExtUtils-Embed
  30. - name: get nginx source
  31.   unarchive:
  32.     src: "{{ url }}"
  33.     dest: "/usr/local/src"
  34.     remote_src: yes
  35. - name: compile and install
  36.   shell:
  37.     cmd: "./configure --prefix={{install_dir}} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make && make install"
  38.     chdir: "/usr/local/src/nginx-{{ version }}"
  39.     creates: "{{install_dir}}/sbin/nginx"
  40. - name: config file
  41.   template:
  42.     src: nginx.conf.j2
  43.     dest: "{{install_dir}}/conf/nginx.conf"
  44.     owner: nginx
  45.     group: nginx
  46.   notify: restart service
  47.   tags:
  48.     - config
  49. - name: create directory
  50.   file:
  51.     path: "{{install_dir}}/conf/conf.d"
  52.     state: directory
  53.     owner: nginx
  54.     group: nginx
  55. - name: change install directory owner
  56.   file:
  57.     path: "{{install_dir}}"
  58.     owner: nginx
  59.     group: nginx
  60.     recurse: yes
  61. - name: copy service file
  62.   template:
  63.     src: nginx.service.j2
  64.     dest: "/lib/systemd/system/nginx.service"
  65. - name: check config
  66.   shell:
  67.     cmd: "{{install_dir}}/sbin/nginx -t"
  68.   register: check_nginx_config
  69.   changed_when:
  70.     - check_nginx_config.stdout.find('successful')
  71.     - false
  72. - name: start service
  73.   systemd:
  74.     daemon_reload: yes
  75.     name: nginx.service
  76.     state: started
  77.     enabled: yes
  78.       
  79. #创建handler文件
  80. [root@ansible-rocky roles]$ cat nginx/handlers/main.yml
  81. - name: restart service
  82.   service:
  83.     name: nginx
  84.     state: restarted
  85. #装备两个template文件
  86. [root@ansible-rocky roles]$ cat nginx/templates/nginx.conf.j2
  87. user nginx;
  88. worker_processes  {{ ansible_processor_vcpus*2 }};
  89. events {
  90.     worker_connections  1024;
  91. }
  92. http {
  93.     include       mime.types;
  94.     default_type  application/octet-stream;
  95.     log_format  access_json '{"@timestamp":"$time_iso8601",'
  96.         '"host":"$server_addr",'
  97.         '"clientip":"$remote_addr",'
  98.         '"size":$body_bytes_sent,'
  99.         '"responsetime":$request_time,'
  100.         '"upstreamtime":"$upstream_response_time",'
  101.         '"upstreamhost":"$upstream_addr",'
  102.         '"http_host":"$host",'
  103.         '"uri":"$uri",'
  104.         '"xff":"$http_x_forwarded_for",'
  105.         '"referer":"$http_referer",'
  106.         '"tcp_xff":"$proxy_protocol_addr",'
  107.         '"http_user_agent":"$http_user_agent",'
  108.         '"status":"$status"}';
  109.     # logging                                                                                          
  110.     access_log {{install_dir}}/logs/access-json.log access_json;
  111.     error_log {{install_dir}}/logs/error.log warn;
  112.     keepalive_timeout  65;
  113.     include {{install_dir}}/conf/conf.d/*.conf;
  114. }
  115. [root@ansible-rocky roles]$ cat nginx/templates/nginx.service.j2
  116. [Unit]
  117. Description=The nginx HTTP and reverse proxy server
  118. After=network.target remote-fs.target nss-lookup.target
  119. [Service]
  120. Type=forking
  121. PIDFile={{install_dir}}/logs/nginx.pid
  122. ExecStartPre=/bin/rm -f {{install_dir}}/logs/nginx.pid
  123. ExecStartPre={{install_dir}}/sbin/nginx -t
  124. ExecStart={{install_dir}}/sbin/nginx
  125. ExecReload=/bin/kill -s HUP \$MAINPID
  126. KillSignal=SIGQUIT
  127. TimeoutStopSec=5
  128. KillMode=process
  129. PrivateTmp=true                                                                                       
  130. LimitNOFILE=100000
  131. [Install]
  132. WantedBy=multi-user.target
  133. #在hosts文件中定义wensrvs需要的变量
  134. [root@ansible-rocky opt]$ cat hosts_nginx
  135. [websrvs]
  136. 10.0.0.18
  137. [websrvs:vars]
  138. version="1.22.1"
  139. url="http://nginx.org/download/nginx-{{ version }}.tar.gz"
  140. install_dir="/apps/nginx"
  141. #在playbook中调用角色
  142. [root@ansible-rocky opt]$ cat nginx_role.yml
  143. - hosts: websrvs
  144.   remote_user: root
  145.   roles:
  146.     - nginx
  147.    
  148. #运行playbook
  149. [root@ansible-rocky opt]$ ansible-playbook -i hosts_nginx nginx_role.yml
  150. PLAY [websrvs] ****************************************************************************************
  151. TASK [Gathering Facts] ********************************************************************************
  152. ok: [10.0.0.18]
  153. TASK [nginx : add group nginx] ************************************************************************
  154. changed: [10.0.0.18]
  155. TASK [nginx : add user nginx] *************************************************************************
  156. changed: [10.0.0.18]
  157. TASK [nginx : install dependent package] **************************************************************
  158. changed: [10.0.0.18] => (item=gcc)
  159. ok: [10.0.0.18] => (item=make)
  160. changed: [10.0.0.18] => (item=pcre-devel)
  161. changed: [10.0.0.18] => (item=openssl-devel)
  162. ok: [10.0.0.18] => (item=zlib-devel)
  163. changed: [10.0.0.18] => (item=perl-ExtUtils-Embed)
  164. TASK [nginx : get nginx source] ***********************************************************************
  165. changed: [10.0.0.18]
  166. TASK [nginx : compile and install] ********************************************************************
  167. changed: [10.0.0.18]
  168. TASK [nginx : config file] ****************************************************************************
  169. changed: [10.0.0.18]
  170. TASK [nginx : create directory] ***********************************************************************
  171. changed: [10.0.0.18]
  172. TASK [nginx : change install directory owner] *********************************************************
  173. changed: [10.0.0.18]
  174. TASK [nginx : copy service file] **********************************************************************
  175. changed: [10.0.0.18]
  176. TASK [nginx : check config] ***************************************************************************
  177. ok: [10.0.0.18]
  178. TASK [nginx : start service] **************************************************************************
  179. changed: [10.0.0.18]
  180. RUNNING HANDLER [nginx : restart service] *************************************************************
  181. changed: [10.0.0.18]
  182. PLAY RECAP ********************************************************************************************
  183. 10.0.0.18                  : ok=13   changed=11   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
复制代码
在被控制端检查是否安装完成

2 编译安装参数详解

编译安装参数示例
  1. ./configure --prefix={{install_dir}} \
  2. --user=nginx \
  3. --group=nginx \
  4. --with-http_ssl_module \
  5. --with-http_v2_module \
  6. --with-http_realip_module \
  7. --with-http_stub_status_module \
  8. --with-http_gzip_static_module \
  9. --with-pcre \
  10. --with-stream \
  11. --with-stream_ssl_module \
  12. --with-stream_realip_module
复制代码
在编译安装参数中--with开头的选项默认是禁用的,想要使用的话就需要在编译的时候加上;without开头的选项默认是开启的,不想要启用此模块的话就需要在编译的时候加上。
通用配置选项参数
选项解释说明--prefix=Nginx安装的根路径,所有其它路径都要依赖该选项--sbin-path=指定nginx二进制文件的路径,没指定的话 这个路径依赖选项--conf-path=命令行未指定配置文件,将会通过这里指定的路径加载配置文件--error-log-path=写入错误日志文件地址,默认值:/logs/error.log。安装后,可以使用 nginx.conf 中的 error_log 指令更改。--pid-path=nginx master进程pid写入的文件位置,默认值:/logs/nginx.pid。安装后,可以使用 nginx.conf 中的 pid 指令更改。--lock-path=共享存储器互斥锁文件路径--user=nginx 运行用户。默认值:nobody。安装后,可以使用 nginx.conf 中的 user 指令更改。--group=nginx 运行组。默认值:--user 指定的值。安装后,可以使用 nginx.conf 中的 user 指令更改。默认开启的模块
选项解释说明--without-http_gzip_module禁用 ngx_http_gzip_module 模块--without-http_userid_module禁用 ngx_http_userid_module 模块,该模块设置适用于客户端标识的 cookie--without-http_access_module禁用 ngx_http_access_module 模块,该模块允许限制对某些客户端地址的访问--without-http_rewrite_module禁用 URL 转发(rewrite)--without-http_proxy_module禁用 HTTP 服务器代理(proxy)模块--without-http-cache禁用 HTTP 缓存默认未开启模块
选项解释说明--with-http_ssl_module启用 HTTPS 协议支持,需要 OpenSSL 库。默认情况下未构建此模块--with-http_v2_module启用 HTTP/2 协议支持。默认情况下未构建此模块。--with-http_realip_module启用 ngx_http_realip_module 模块的功能,该模块将客户端地址更改为在指定的 "header " 字段中发送的地址。默认情况下未构建此模块--with-http_sub_module启用 ngx_http_sub_module 模块,该模块通过将一个指定的字符串替换为另一个指定的字符串来修改响应。默认情况下未构建此模块--with-http_gzip_static_module启用 ngx_http_gzip_static_module 模块,该模块支持发送扩展名为 “.gz” 的预压缩文件,而不是常规文件。默认情况下未构建此模块--with-http_auth_request_module启用 ngx_http_auth_request_module 模块,该模块基于子请求的结果实现客户端授权。默认情况下未构建此模块--with-http_stub_status_module启用 ngx_http_stub_status_module 模块,该模块提供对基本状态信息的访问。默认情况下未构建此模块--add-module=path启用外部模块--add-dynamic-module=path启用外部动态模块--modules-path=pathnginx 动态模块的目录。默认值:/modules目录perl模块相关选项参数
选项解释说明--without-pcre禁用PCRE库--with-pcre强制使用PCRE库邮件模块相关配置选项参数
选项解释说明--with-mail激活POP3/IMAP4/SMTP代理模块,默认未激活--with-mail_ssl_module允许ngx_mail_ssl_module模块这个模块使得POP3/IMAP/SMTP可以使用SSL/TLS.配置已经定义了HTTP SSL模块,但是不支持客户端证书检测--without-mail_pop3_module启用mail模块后,单独禁用pop3模块--without-mail_imap_module启用mail模块后,单独禁用imap模块--without-mail_smtp_module启用mail模块后,单独禁用smtp模块--without-http完全禁用http模块,如果只想支持mall,可以使用此项设置--with-openssl=DIR设定OpenSSL库文件路径stream模块相关参数
选项解释说明--with-stream开启stream模块--with-stream_ssl_module启用 stream 模块的 SSL/TLS 协议支持。构建和运行此模块需要 OpenSSL 库。默认情况下未构建此模块--with-stream_realip_module启用 ngx_stream_realip_module 模块的功能,该模块将客户端地址更改为 PROXY 协议标头中发送的地址。默认情况下未构建此模块--without-stream_access_module禁用 ngx_stream_access_module 模块,该模块允许限制对某些客户端地址的访问
来源:https://www.cnblogs.com/yan-linux/p/17043491.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具