網站首頁 編程語言 正文
1 基于ansible role實現編譯安裝nginx
利用ansible控制端10.0.0.8機器,在被控制端10.0.0.18上部署nginx
首先打通ansible控制端與被控制端的基于key驗證
[root@ansible-rocky ~]$ ssh-copy-id 10.0.0.18 [root@ansible-rocky ~]$ ssh 10.0.0.18 Last login: Wed Jan 11 12:18:28 2023 from 10.0.0.8 [root@rocky8 ~]$ hostname -I 10.0.0.18
然后創建nginx項目目錄實現基于role的部署任務
#nginx role項目目錄總覽 [root@ansible-rocky opt]$ tree /opt /opt ├── hosts_nginx ├── nginx_role.yml └── roles └── nginx ├── handlers │ └── main.yml ├── tasks │ └── main.yml └── templates ├── nginx.conf.j2 └── nginx.service.j2 #task文件 [root@ansible-rocky roles]$ cat nginx/tasks/main.yml - name: add group nginx group: name=nginx system=yes gid=80 - name: add user nginx user: name=nginx group=nginx uid=80 system=yes shell="/sbin/nologin" create_home=no - name: install dependent package yum: name={{item}} state=latest loop: - gcc - make - pcre-devel - openssl-devel - zlib-devel - perl-ExtUtils-Embed - name: get nginx source unarchive: src: "{{ url }}" dest: "/usr/local/src" remote_src: yes - name: compile and install shell: 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" chdir: "/usr/local/src/nginx-{{ version }}" creates: "{{install_dir}}/sbin/nginx" - name: config file template: src: nginx.conf.j2 dest: "{{install_dir}}/conf/nginx.conf" owner: nginx group: nginx notify: restart service tags: - config - name: create directory file: path: "{{install_dir}}/conf/conf.d" state: directory owner: nginx group: nginx - name: change install directory owner file: path: "{{install_dir}}" owner: nginx group: nginx recurse: yes - name: copy service file template: src: nginx.service.j2 dest: "/lib/systemd/system/nginx.service" - name: check config shell: cmd: "{{install_dir}}/sbin/nginx -t" register: check_nginx_config changed_when: - check_nginx_config.stdout.find('successful') - false - name: start service systemd: daemon_reload: yes name: nginx.service state: started enabled: yes #創建handler文件 [root@ansible-rocky roles]$ cat nginx/handlers/main.yml - name: restart service service: name: nginx state: restarted #裝備兩個template文件 [root@ansible-rocky roles]$ cat nginx/templates/nginx.conf.j2 user nginx; worker_processes {{ ansible_processor_vcpus*2 }}; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; # logging access_log {{install_dir}}/logs/access-json.log access_json; error_log {{install_dir}}/logs/error.log warn; keepalive_timeout 65; include {{install_dir}}/conf/conf.d/*.conf; } [root@ansible-rocky roles]$ cat nginx/templates/nginx.service.j2 [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile={{install_dir}}/logs/nginx.pid ExecStartPre=/bin/rm -f {{install_dir}}/logs/nginx.pid ExecStartPre={{install_dir}}/sbin/nginx -t ExecStart={{install_dir}}/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true LimitNOFILE=100000 [Install] WantedBy=multi-user.target #在hosts文件中定義wensrvs需要的變量 [root@ansible-rocky opt]$ cat hosts_nginx [websrvs] 10.0.0.18 [websrvs:vars] version="1.22.1" url="http://nginx.org/download/nginx-{{ version }}.tar.gz" install_dir="/apps/nginx" #在playbook中調用角色 [root@ansible-rocky opt]$ cat nginx_role.yml - hosts: websrvs remote_user: root roles: - nginx #運行playbook [root@ansible-rocky opt]$ ansible-playbook -i hosts_nginx nginx_role.yml PLAY [websrvs] **************************************************************************************** TASK [Gathering Facts] ******************************************************************************** ok: [10.0.0.18] TASK [nginx : add group nginx] ************************************************************************ changed: [10.0.0.18] TASK [nginx : add user nginx] ************************************************************************* changed: [10.0.0.18] TASK [nginx : install dependent package] ************************************************************** changed: [10.0.0.18] => (item=gcc) ok: [10.0.0.18] => (item=make) changed: [10.0.0.18] => (item=pcre-devel) changed: [10.0.0.18] => (item=openssl-devel) ok: [10.0.0.18] => (item=zlib-devel) changed: [10.0.0.18] => (item=perl-ExtUtils-Embed) TASK [nginx : get nginx source] *********************************************************************** changed: [10.0.0.18] TASK [nginx : compile and install] ******************************************************************** changed: [10.0.0.18] TASK [nginx : config file] **************************************************************************** changed: [10.0.0.18] TASK [nginx : create directory] *********************************************************************** changed: [10.0.0.18] TASK [nginx : change install directory owner] ********************************************************* changed: [10.0.0.18] TASK [nginx : copy service file] ********************************************************************** changed: [10.0.0.18] TASK [nginx : check config] *************************************************************************** ok: [10.0.0.18] TASK [nginx : start service] ************************************************************************** changed: [10.0.0.18] RUNNING HANDLER [nginx : restart service] ************************************************************* changed: [10.0.0.18] PLAY RECAP ******************************************************************************************** 10.0.0.18 : ok=13 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在被控制端檢查是否安裝完成
2 編譯安裝參數詳解
編譯安裝參數示例:
./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
在編譯安裝參數中--with開頭的選項
默認是禁用的,想要使用的話就需要在編譯的時候加上;without開頭的選項
默認是開啟的,不想要啟用此模塊的話就需要在編譯的時候加上。
通用配置選項參數:
選項 | 解釋說明 |
---|---|
--prefix=<path> | Nginx安裝的根路徑,所有其它路徑都要依賴該選項 |
--sbin-path=<path> | 指定nginx二進制文件的路徑,沒指定的話 這個路徑依賴<prefix>選項 |
--conf-path=<path> | 命令行未指定配置文件,將會通過這里指定的路徑加載配置文件 |
--error-log-path=<path> | 寫入錯誤日志文件地址,默認值:<prefix>/logs/error.log。安裝后,可以使用 nginx.conf 中的 error_log 指令更改。 |
--pid-path=<path> | nginx master進程pid寫入的文件位置,默認值:<prefix>/logs/nginx.pid。安裝后,可以使用 nginx.conf 中的 pid 指令更改。 |
--lock-path=<path> | 共享存儲器互斥鎖文件路徑 |
--user=<user> | nginx 運行用戶。默認值:nobody。安裝后,可以使用 nginx.conf 中的 user 指令更改。 |
--group=<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=path | nginx 動態模塊的目錄。默認值:<prefix>/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/archive/2023/01/11/17043491.html
相關推薦
- 2022-04-12 C++中菱形繼承的解釋與處理詳解_C 語言
- 2022-11-02 React循環遍歷渲染數組和對象元素方式_React
- 2022-08-18 python高階函數使用教程示例_python
- 2022-10-30 Python?Pandas學習之series的二元運算詳解_python
- 2023-08-01 elementui中Table切換分頁保存多選框選中的數據
- 2022-07-16 Linux查看日志的幾種方式
- 2022-12-06 Python中八種數據導入方法總結_python
- 2022-05-25 文字解說Golang?Goroutine和線程的區別_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支