Practical operation:
Idea: first download nginx package on one computer, and then distribute it with ansible
Download and install nginx first
wget http://mirrors.sohu.com/nginx/nginx-1.9.6.tar.gz
Unzip file
tar -zxvf nginx-1.9.6.tar.gz
First in directory
cd nginx-1.9.6
Download and install plug-ins
yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y
to configure
./configure --prefix=/usr/local/nginx
Edit / etc / init D / nginx file
vi /etc/init.d/nginx
Content:
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usx/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
Compile and install
make && make install
Check whether the compilation and installation are correct
echo $?
Empty profile
> /usr/local/nginx/conf/nginx.conf
Edit / usr / local / nginx / conf / nginx Conf file
vi /usr/local/nginx/conf/nginx.conf
Content:
user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name; } } }
Check it
/usr/local/nginx/sbin/nginx -t
View port
netstat -ntlp
Check whether port 80 is occupied and turn off the corresponding service.
systemcl stop httpd
Give permission first
chmod 777 /etc/init.d/nginx
Restart on
service nginx start
First enter / etc/ansible / to create a directory
cd /etc/ansible/ mkdir nginx_install
After entering the newly created directory, continue to create the directory
cd nginx_install mkdir -p roles/{common,install}/{handlers,files,meta,tasks,templates,vars}
1, There are two roles in the roles directory.
1. common prepares for some operations.
2 . install is the operation of installing nginx. There are several directories under each role,
(1) The following are the operations to be performed when the configuration file changes. It is usually used to restart the service when the configuration file changes. Files are some files used during installation.
(2) meta is the description information, which describes the role dependency and other information.
(3) tasks is the core configuration file.
(4) templates usually saves some configuration files, startup scripts and other template files
(5) var is the defined variable
Move the files we need to get to the created directory.
cd /usr/local/ ls tar -zcvf nginx.tar.gz nginx/ mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/ ls cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/ cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/
Write the document after moving.
To define common tasks, nginx needs some dependent packages.
vim /etc/ansible/nginx_install/roles/common/tasks/main.yml - name: install initializtion requre software yum: name={{ item }} state=installed with_items: - zlib-devel - pcre-devel
Define variables
vi /etc/ansible/nginx_install/roles/install/vars/main.yml nginx_user: www nginx_port: 80 nginx_basedir: /usr/local/nginx
Copy all documents to the target machine
vi /etc/ansible/nginx_install/roles/install/tasks/copy.yml - name: Copry Nginx Software copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root - name: Uncompression Nginx Softeare shell: tar -zxf /tmp/nginx.tar.gz -C /usr/local - name: Copy Nginx STart Script template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755 - name: Copy Nginx Config template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644
The user did not start the service to delete the compressed package.
vi /etc/ansible/nginx_install/roles/install/tasks/install.yml - name: Create Nginx User user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin - name: Start Nginx Service shell: /etc/init.d/nginx start - name: Add Boot Start Nginx Service shell: chkconfig --level 345 nginx on - name: Delet Nginx compression files shell: rm -rf /tmp/nginx.tar.gz
After creating main install and copy
vi /etc/ansible/nginx_install/roles/install/tasks/main.yml - include: copy.yml - include: install.yml
Define the portal profile.
vi /etc/ansible/nginx_install/install.yml --- - hosts: 10.30.59.210 remote_user: root gather_facts: True roles: - common - install
Execute playbook
ansible-playbook /etc/ansible/nginx_install/install.yml
Fourth, manage and configure nginx
In most production environments, configuration files need to be managed. The installation package is only used when initializing the environment,
First create
mkdir -p /etc/ansible/nginx_config/roles/{new,old}/{files,handlers,vars,tasks}
Note:
Where new is used for updating, old is used for rollback, and nginx is under file Conf directory and vhosts directory. handlers is the command to restart nginx service.
For rollback, you need to back up the old configuration file before executing playbook. The management of the old configuration file must be strict. You must not arbitrarily modify the configuration of the online machine, and ensure that the configuration files under new and file are consistent with the online configuration file.
Because we don't have a vhosts file to create for you.
cd /usr/local/nginx/conf/ mkdir vhosts
After entering the configuration file, modify the configuration. Both virtual machines need to be configured.
vi nginx.conf
Content: (added second from last)
{ include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name; } } include /usr/local/nginx/conf/vhosts/*.conf; }
cp copy files.
cp -r nginx.conf vhosts /etc/ansible/nginx_config/roles/new/files/
Edit yml file
vi /etc/ansible/nginx_config/roles/new/vars/main.yml
Content: (a path is defined)
nginx_basedir: /usr/local/nginx
Continue compiling the yml file
vi /etc/ansible/nginx_config/roles/new/handlers/main.yml
Content: (restart nginx service)
- name: restsart nginx shell: /etc/init.d/nginx reload
After continuing to edit a file, put nginx CNF and vhosts virtual host copy to the place you want. Defines users, groups, and permissions. Core document
vi /etc/ansible/nginx_config/roles/new/tasks/main.yml
Content:
- name: copy conf file copy: src={{ item.src }} dest={{ nginx_basedir }}/{{ item.dest }} backup=yes owner=root group=root mode=0644 with_items: - { src: nginx.conf, dest: conf/nginx.conf } - { src: vhosts, dest: conf/ } notify: restart ngin continue editing yml file ```objectivec vi /etc/ansible/nginx_config/update.yml
Content:
--- - hosts: testhost user: root roles: - new
Editing the yml file. Portal profile
vi /etc/ansible/nginx_config/update.yml
Content:
--- - hosts: testhost user: root roles: - new
Execution:
ansible-playbook /etc/ansible/nginx_config/update.yml
Set rollback. The rollback operation is to overwrite the old configuration. Then reload the nginx service and back up the nginx configuration file to the old before each change. The corresponding directory is / etc/ansible/nginx_conf/rollback.yml
(define general entrance configuration)
Copy backup yml
rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/
(define general entrance configuration)
vi /etc/ansible/nginx_config/rollback.yml --- - hosts: testhost user: root roles: - old
Execution:
ansible-playbook /etc/ansible/nginx_config/rollback.yml