---
- hosts: 10.0.0.171
vars:
nginxver: 1.18.0
nginxsrc: "http://nginx.org/download/nginx-{{nginxver}}.tar.gz"
srcdir: "/usr/local/src/nginx-{{nginxver}}"
appdir: "/apps/nginx-{{nginxver}}"
tasks:
- name: get cpus
shell: lscpu | awk '/^CPU\(s\)/{print $2}'
register: cpus
- name: install necessary packs
yum:
name:
- gcc
- pcre-devel
- openssl-devel
- zlib-devel
- name: download nginx and unarchive
unarchive: remote_src=yes src={{nginxsrc}} dest=/usr/local/src
register: var1
until: var1 is succeeded
retries: 5
- name: create user nginx
user: name=nginx state=present shell=/sbin/nologin system=yes
- name: create dir
file: path={{appdir}} state=directory owner=nginx group=nginx
- name: configure
shell: |
chdir={{srcdir}}
./configure --prefix={{appdir}} \
--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 -j {{cpus.stdout}} && make install
- name: chown
file: path={{appdir}} state=directory owner=nginx group=nginx recurse=yes
- name: create soft link
file: src={{appdir}}/sbin/nginx path=/usr/bin/nginx state=link
- name: create service
copy:
content: |
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile={{appdir}}/run/nginx.pid
ExecStart={{appdir}}/sbin/nginx -c {{appdir}}/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
dest: /usr/lib/systemd/system/nginx.service
- name: create pid dir
file: path={{appdir}}/run/ state=directory owner=nginx group=nginx
- name: change config file1
lineinfile: path={{appdir}}/conf/nginx.conf regexp="^#pid" line="pid {{appdir}}/run/nginx.pid;"
- name: change config file2
lineinfile: path={{appdir}}/conf/nginx.conf regexp="^#user" line="user nginx nginx;"
- name: change config file3
lineinfile: path={{appdir}}/conf/nginx.conf regexp="^worker_processes" line="worker_processes {{cpus.stdout}};"
- name: start nginx
service: name=nginx state=started enabled=yes
ansible编译安装nginx
最新推荐文章于 2024-05-06 10:54:58 发布