1. 引言
Nginx 是一个高性能的开源 Web 服务器和反向代理服务器,通过使用 Ansible,您可以实现自动化部署和配置 Nginx,简化服务器的设置和管理,并提高系统的可靠性和性能。
2. Nginx 的使用场景
Nginx 在以下场景中特别有用:
2.1 Web 服务器
Nginx 是一个强大的 Web 服务器,可以处理大量并发连接,提供静态内容、动态内容以及负载均衡等功能。通过部署和配置 Nginx,您可以轻松构建和运行高性能的 Web 应用程序,满足用户的并发请求。
2.2 反向代理服务器
Nginx 作为反向代理服务器,可以将来自客户端的请求转发到后端的多个服务器,实现负载均衡和高可用性。通过使用 Nginx 的反向代理功能,您可以有效地分发流量,并在后端服务器之间实现请求的智能路由。
2.3 静态资源服务器
Nginx 也可以作为静态资源服务器使用,例如图片、CSS 和 JavaScript 文件等。通过将静态资源与动态资源分离,Nginx 可以提供更高的性能和吞吐量,减轻后端应用服务器的负载。
2.4 缓存服务器
Nginx 具有内置的缓存功能,可以缓存静态和动态内容,减少对后端应用服务器的请求。通过配置 Nginx 的缓存机制,您可以提高响应速度,减少对后端资源的依赖,提供更好的用户体验。
3. Ansible 部署 Nginx 的最佳实践
下面是使用 Ansible 部署 Nginx 的最佳实践步骤:
3.1 安装 Ansible
首先,请确保您的系统已安装 Ansible。您可以根据您的操作系统类型和版本,参考 Ansible 官方文档中的安装指南进行安装。
3.2 创建 Ansible Inventory
创建一个名为 deploy-nginx-host.ini 的 Ansible Inventory 配置文件
[deploy]
aliyun_ecs ansible_ssh_host=47.97.194.83 ansible_ssh_port=22 ansible_user=ecs-user ansible_ssh_pass=Ecs-user123 ansible_sudo_pass=Ecs-user123
3.3 创建 Nginx 配置文件
创建一个名为 nginx.conf.j2 的 nginx 配置文件
user {{ nginx_install_user }};
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.4 创建开机启动文件
创建一个名为 nginx-supervisord.ini.j2的 Supervisor 启动文件
[program:nginx]
directory=/home/{{ nginx_install_user }}/{{ nginx_home_dir }} ;
command=/home/{{ nginx_install_user }}/{{ nginx_home_dir }}/sbin/nginx -c /home/{{ nginx_install_user }}/{{ nginx_home_dir }}/conf/nginx.conf -g 'daemon off';
stopsignal=KILL ;
stderr_logfile=/var/log/supervisor/nginx.err ;
stdout_logfile=/var/log/supervisor/nginx.out ;
stdout_logfile_maxbytes=10MB ;
stdout_logfile_backups=10 ;
user={{ nginx_install_user }} ;
umask=022 ;
autostart=true ;
autorestart=true ;
startsecs=10 ;
startretries=3 ;
stopasgroup=true ;
killasgroup=true ;
redirect_stderr=false ;
3.5 创建 Ansible Playbook
创建一个名为 deploy-nginx.yml 的 Ansible Playbook 文件,并使用文本编辑器打开。在 Playbook 中,定义以下任务:
---
- name: 安装 Nginx
hosts: deploy
become: true
vars:
nginx_version: 1.24.0
nginx_home_dir: "nginx-{{ nginx_version }}"
nginx_source_tar_name: "{{ nginx_home_dir }}.tar.gz"
nginx_install_user: ecs-user
tasks:
- name: 下载 Linux 通用包
ansible.builtin.get_url:
url: "http://i-ansible.oss-cn-hangzhou-internal.aliyuncs.com/{{ nginx_source_tar_name }}"
validate_certs: false
dest: "/tmp/{{ nginx_source_tar_name }}"
mode: "0644"
force: true
owner: "{{ nginx_install_user }}"
group: "{{ nginx_install_user }}"
- name: 解压源码
ansible.builtin.unarchive:
src: "/tmp/{{ nginx_source_tar_name }}"
dest: "/usr/local/src/"
remote_src: true
owner: "{{ nginx_install_user }}"
group: "{{ nginx_install_user }}"
become_user: root
- name: 安装编译器
ansible.builtin.package:
name: "{{ item }}"
with_items:
- gcc
- gcc-c++
# - kernel-devel
- pcre-devel
- zlib-devel
- name: 编译源码
ansible.builtin.command:
chdir: "/usr/local/src/{{ nginx_home_dir }}"
cmd: >
./configure --prefix=/home/{{ nginx_install_user }}/{{ nginx_home_dir }} --user={{ nginx_install_user }} --group={{ nginx_install_user }}
changed_when: false
- name: Make
ansible.builtin.command:
chdir: "/usr/local/src/{{ nginx_home_dir }}"
cmd: make
changed_when: false
- name: Make Install
ansible.builtin.command:
chdir: "/usr/local/src/{{ nginx_home_dir }}"
cmd: make install
changed_when: false
- name: Config Permision
ansible.builtin.command: "chown -R {{ nginx_install_user }}:{{ nginx_install_user }} {{ item }}"
with_items:
- "/home/{{ nginx_install_user }}/{{ nginx_home_dir }}"
changed_when: false
- name: 普通用户开通 80 端口
ansible.builtin.command: setcap 'cap_net_bind_service=+ep' /home/{{ nginx_install_user }}/{{ nginx_home_dir }}/sbin/nginx
changed_when: false
- name: 复制启动文件
ansible.builtin.template:
src: "{{ item }}.j2"
dest: "/etc/supervisord.d/conf.d/{{ item }}"
mode: "0755"
owner: root
group: root
with_items:
- nginx-supervisord.ini
- name: 更新启动文件
ansible.builtin.command:
chdir: /tmp
cmd: /usr/bin/supervisorctl -c /etc/supervisord.d/supervisor.conf update
changed_when: false
- name: 重启
ansible.builtin.command:
chdir: /tmp
cmd: "/usr/bin/supervisorctl -u admin -p 123456 restart nginx"
changed_when: false
- name: 检查端口是否运行
ansible.builtin.wait_for:
port: 80
state: started
delay: 1
timeout: 30
- name: 测试访问nginx
ansible.builtin.uri:
url: "http://127.0.0.1"
return_content: true
status_code: 200
register: curl_info
- name: 打印请求信息
ansible.builtin.debug:
var: curl_info.msg
changed_when: false
3.6 执行 Ansible Playbook
在终端中,导航到包含 Playbook 的目录,并运行以下命令来执行 Ansible Playbook:
$ ansible-playbook -i deploy-nginx-host.ini deploy-nginx.yml
PLAY [安装 Nginx] *****************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************
ok: [aliyun_ecs]
TASK [下载 Linux 通用包] **********************************************************************************************************************
ok: [aliyun_ecs]
TASK [解压源码] *******************************************************************************************************************************
ok: [aliyun_ecs]
TASK [安装编译器] *****************************************************************************************************************************
ok: [aliyun_ecs] => (item=gcc)
ok: [aliyun_ecs] => (item=gcc-c++)
ok: [aliyun_ecs] => (item=pcre-devel)
ok: [aliyun_ecs] => (item=zlib-devel)
TASK [编译源码] *******************************************************************************************************************************
ok: [aliyun_ecs]
TASK [Make] ***********************************************************************************************************************************
ok: [aliyun_ecs]
TASK [Make Install] ***************************************************************************************************************************
ok: [aliyun_ecs]
TASK [Config Permision] ***********************************************************************************************************************
ok: [aliyun_ecs] => (item=/home/ecs-user/nginx-1.24.0)
TASK [普通用户开通 80 端口] *******************************************************************************************************************
ok: [aliyun_ecs]
TASK [复制启动文件] ***************************************************************************************************************************
ok: [aliyun_ecs] => (item=nginx-supervisord.ini)
TASK [更新启动文件] ***************************************************************************************************************************
ok: [aliyun_ecs]
TASK [重启] ***********************************************************************************************************************************
ok: [aliyun_ecs]
TASK [检查端口是否运行] ***********************************************************************************************************************
ok: [aliyun_ecs]
TASK [测试访问nginx] **************************************************************************************************************************
ok: [aliyun_ecs]
TASK [打印请求信息] ***************************************************************************************************************************
ok: [aliyun_ecs] => {
"curl_info.msg": "OK (615 bytes)"
}
PLAY RECAP ************************************************************************************************************************************
aliyun_ecs : ok=15 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible 将在目标主机上安装和配置 Nginx,并启动 Nginx 服务。您可以检查目标主机上的 Nginx 进程和服务状态,确保一切正常。
3.7 测试
4. 结论
本文介绍了使用 Ansible 部署 Nginx 的最佳实践。通过自动化配置和部署 Nginx,您可以简化服务器的设置和管理过程,提高系统的可靠性和性能。Nginx 在 Web 服务器、反向代理服务器、静态资源服务器和缓存服务器等场景中具有广泛的应用。使用 Ansible Playbook可以轻松地实现 Nginx 的自动化部署,提高工作效率和系统的可靠性。希望本文对您有所帮助,祝您成功使用 Ansible 部署和配置 Nginx!