当前位置:Gxlcms > PHP教程 > ansible自动化远程编译启动nginx

ansible自动化远程编译启动nginx

时间:2021-07-01 10:21:17 帮助过:13人阅读

这篇文章主要介绍了关于ansible自动化远程编译启动nginx,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

环境:rhel7.3
软件:

  1. [root@server11 ~]# ansible --versionansible 2.5.3
  2. config file = /etc/ansible/ansible.cfg
  3. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  4. ansible python module location = /usr/lib/python2.7/site-packages/ansible
  5. executable location = /usr/bin/ansible
  6. python version = 2.7.5 (default, Aug 2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

ssh 免密

  1. [root@server11 ansible]# ssh-keygen[root@server11 ansible]# ssh-copy-id 192.168.122.12

文件目录结构

  1. [root@server11 ansible]# tree.
  2. ├── ansible.cfg├── hosts
  3. ├── nginx.yaml└── roles
  4. └── nginx
  5. ├── default
  6. ├── files
  7. │ └── nginx-1.13.6.tar.gz
  8. ├── handlers
  9. ├── meta
  10. ├── tasks
  11. │ └── main.yml
  12. ├── templates
  13. │ ├── nginx.conf
  14. │ └── nginx.service
  15. └── vars
  16. └── main.yml9 directories, 8 files

roles/nginx/tasks/main.yml

  1. [root@server11 ansible]# cat roles/nginx/tasks/main.yml - name: copy package
  2. copy: src=nginx-1.13.6.tar.gz dest=/usr/local/src/nginx-1.13.6.tar.gz
  3. tags: cppkg
  4. - name: tar nginx
  5. shell: cd /usr/local/src;tar -xf nginx-1.13.6.tar.gz
  6. - name: yum install
  7. yum: name={{ item }} state=latest
  8. with_items:
  9. - openssl-devel
  10. - pcre-devel
  11. - gcc
  12. - name: install nginx
  13. shell: useradd nginx;cd /usr/local/src/nginx-1.13.6;./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre;make && make install - name: copy conf file
  14. template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
  15. - name: systemctl init
  16. template: src=nginx.service dest=/usr/lib/systemd/system/nginx.service
  17. - name: start nginx service
  18. service: name=nginx state=started enabled=true

roles/nginx/templates/nginx.conf

  1. [root@server11 ansible]# cat roles/nginx/templates/nginx.conf user nginx;
  2. worker_processes {{ ansible_processor_vcpus }};
  3. #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events {
  4. worker_connections 65535;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  10. # '$status $body_bytes_sent "$http_referer" '
  11. # '"$http_user_agent" "$http_x_forwarded_for"';
  12. #access_log logs/access.log main;
  13. sendfile on;
  14. #tcp_nopush on;
  15. #keepalive_timeout 0;
  16. keepalive_timeout 65;
  17. #gzip on;
  18. server {
  19. listen {{ nginxport }};
  20. server_name localhost;
  21. #charset koi8-r;
  22. #access_log logs/host.access.log main;
  23. location / {
  24. root html;
  25. index index.html index.htm;
  26. }
  27. #error_page 404 /404.html;
  28. # redirect server error pages to the static page /50x.html
  29. #
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  35. #
  36. #location ~ \.php$ {
  37. # proxy_pass http://127.0.0.1;
  38. #}
  39. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  40. #
  41. #location ~ \.php$ {
  42. # root html;
  43. # fastcgi_pass 127.0.0.1:9000;
  44. # fastcgi_index index.php;
  45. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  46. # include fastcgi_params;
  47. #}
  48. # deny access to .htaccess files, if Apache's document root
  49. # concurs with nginx's one
  50. #
  51. #location ~ /\.ht {
  52. # deny all;
  53. #}
  54. }
  55. # another virtual host using mix of IP-, name-, and port-based configuration
  56. #
  57. #server {
  58. # listen 8000;
  59. # listen somename:8080;
  60. # server_name somename alias another.alias;
  61. # location / {
  62. # root html;
  63. # index index.html index.htm;
  64. # }
  65. #}
  66. # HTTPS server
  67. #
  68. #server {
  69. # listen 443 ssl;
  70. # server_name localhost;
  71. # ssl_certificate cert.pem;
  72. # ssl_certificate_key cert.key;
  73. # ssl_session_cache shared:SSL:1m;
  74. # ssl_session_timeout 5m;
  75. # ssl_ciphers HIGH:!aNULL:!MD5;
  76. # ssl_prefer_server_ciphers on;
  77. # location / {
  78. # root html;
  79. # index index.html index.htm;
  80. # }
  81. #} }

roles/nginx/templates/nginx.service

  1. [root@server11 ansible]# cat roles/nginx/templates/nginx.service [Unit]
  2. Description=The nginx HTTP and reverse proxy server
  3. After=network.target remote-fs.target nss-lookup.target
  4. [Service]
  5. Type=forking
  6. PIDFile=/usr/local/nginx/logs/nginx.pid
  7. ExecStartPre=/usr/bin/rm -f /run/nginx.pid
  8. ExecStartPre=/usr/local/nginx/sbin/nginx -t
  9. ExecStart=/usr/local/nginx/sbin/nginx
  10. ExecReload=/bin/kill -s HUP $MAINPID
  11. KillMode=process
  12. KillSignal=SIGQUIT
  13. TimeoutStopSec=5
  14. PrivateTmp=true
  15. [Install]
  16. WantedBy=multi-user.target

roles/nginx/vars/main.yml

  1. [root@server11 ansible]# cat roles/nginx/vars/main.yml nginxport: "80" server_name: "192.168.122.12" root_dir: "/web"

nginx.yaml

  1. [root@server11 ansible]# cat nginx.yaml
  2. - hosts: 192.168.122.12 remote_user: root
  3. roles:
  4. - nginx

相关推荐:

ansible api实现命令异步执行

以上就是ansible自动化远程编译启动nginx的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行