目录 1 基于ansible role实现编译安装nginx 2 编译安装参数详解 1 基于ansible role实现编译安装nginx 利用ansible控制端10.0.0.8机器,在被控制端10.0.0.18上部署nginx 首先打通ansible控制端与被控制端
目录
- 1 基于ansible role实现编译安装nginx
- 2 编译安装参数详解
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开头的选项
默认是开启的,不想要启用此模块的话就需要在编译的时候加上。
通用配置选项参数:
--user
指定的值。安装后,可以使用 nginx.conf 中的 user 指令更改。默认开启的模块
默认未开启模块
perl模块相关选项参数
邮件模块相关配置选项参数
stream模块相关参数