方法一:部分使用ansible
基于現有的nginx配置文件,定制部署nginx軟件,將我們的知識進行整合 定制要求:
啟動用戶:nginx-test,uid是82,系統用戶,不能登錄
啟動端口82 web項目根目錄/data/webserver/html
默認首頁:index.html
首頁內容:"welcome to ansible"
1.1 準備工作?
- 前提:三臺系統都是rocky
檢查系統:確保三臺主機的 Rocky 系統已聯網,能正常安裝軟件包。 - 關閉防火墻和 SELinux?(測試環境建議關閉,生產環境按需配置規則):
關閉防火墻:systemctl stop firewalld;systemctl disable firewalld
永久關閉 SELinux(需重啟生效):編輯?/etc/selinux/config
?文件,將?SELINUX=enforcing
?改為?SELINUX=disabled
?
1.2?創建啟動用戶
在每臺主機上執行以下命令創建?nginx-test
?用戶,且設置為系統用戶、不能登錄:
useradd -u 82 -s /sbin/nologin nginx-test
1.3?安裝 Nginx
yum install nginx -y??
1.4 配置 Nginx
修改配置文件:打開 Nginx 的主配置文件(yum 安裝一般在?/etc/nginx/nginx.conf)
user nginx-test; # 修改啟動用戶
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 1024;
}http {server {listen 82; # 修改監聽端口為82server_name _;root /data/webserver/html; # 設置項目根目錄index index.html; # 設置默認首頁location / {try_files $uri $uri/ =404;}}include /etc/nginx/mime.types;default_type application/octet-stream;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;gzip on;
}
- 創建項目根目錄及首頁文件:
mkdir -p /data/webserver/html echo "welcome to ansible" > /data/webserver/html/index.html chown -R nginx-test:nginx-test /data/webserver/html # 設置目錄所有者
1.5?啟動 Nginx?
systemctl start nginx;systemctl enable nginx # 設置開機自啟
1.6 測試訪問
創建nginx_deploy.yml文件并輸入下面的內容并使用?ansible-playbook nginx_deploy.yml
?執行
- hosts: 10.0.0.12,10.0.0.15,10.0.0.18become: truetasks:- name: Create nginx-test useruser:name: nginx-testuid: 82shell: /sbin/nologin- name: Install nginxyum:name: nginxstate: present- name: Configure nginx.conflineinfile:path: /etc/nginx/nginx.confregexp: "{{ item.regexp }}"line: "{{ item.line }}"create: trueloop:- { regexp: "^user", line: "user nginx-test;" }- { regexp: "^listen", line: "listen 82;" }- { regexp: "^root", line: "root /data/webserver/html;" }- { regexp: "^index", line: "index index.html;" }- name: Create web project root directoryfile:path: /data/webserver/htmlstate: directoryowner: nginx-testgroup: nginx-test- name: Create index.htmlcopy:content: "welcome to ansible"dest: /data/webserver/html/index.htmlowner: nginx-testgroup: nginx-test- name: Start nginxservice:name: nginxstate: startedenabled: true
方法二:完全用ansible實現自動化
注意:按照方法一 前提環境已部署好,防護墻,selinux等 這里就不再操作
1.1 制作?個nginx.conf
server {listen 10086;root /data/webserver/html;location / {index index.html; # 添加默認首頁try_files $uri $uri/ =404; # 添加請求處理規則}
}
1.2 編寫playbook? ?
- hosts: webremote_user: roottasks:- name: create new useruser:name: nginx-testsystem: yesuid: 82shell: /sbin/nologin- name: create web rootfile:name: /data/webserver/htmlowner: nginx-teststate: directory- name: touch web indexshell: echo '<h1>welcome to ansible</h1>' > /data/webserver/html/index.html- name: install packageyum:name: nginxstate: present- name: copy configcopy:src: nginx.confdest: /etc/nginx/nginx.conf- name: copy subconfigcopy:src: nginx-define.confdest: /etc/nginx/conf.d- name: start serviceservice:name: nginxstate: startedenabled: yes
1.3 檢測執行效果

若出現都是80端口,則還需要執行??ansible web -m shell -a "systemctl reload nginx"
?