Configure webserver with nginx
We create an Ansible playbook named web-notls.yml.
Type the following commands:
- name: Configure webserver with nginx
hosts: host1
become: yes
become_user: root
become_method: su
tasks:
- name: install nginx
yum: name=nginx
- name: create sites-available directory
file: path=/etc/nginx/sites-available state=directory mode=0755
- name: copy nginx config file
copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
- name: create sites-enabled directory
file: path=/etc/nginx/sites-enabled state=directory mode=0755
- name: enable configuration
file: >
dest=/etc/nginx/sites-enabled/default
src=/etc/nginx/sites-available/default
state=link
- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644
- name: restart nginx
service: name=nginx state=restarted
We need to extra files.
1) files/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
}
2) templates/index.html.j2
Welcome to ansible
nginx, configured by Ansible
If you can see this, Ansible successfully installed nginx.
{{ ansible_managed }}
Then, we run ansible-playbook and got an error:
TASK [restart nginx] *****************************************************************************************************************************
fatal: [host1]: FAILED! => {"changed": false, "failed": true, "msg": "Unable to start service nginx: Job for nginx.service failed because the control process exited with error code. See \"systemctl status nginx.service\" and \"journalctl -xe\" for details.
"}
Why is that?
After running
systemctl status nginx.service
The output is
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
If we run
lsof-i -P -n
It is found that
httpd ....................TCP *:80 (LISTEN)
Port 80 was used by httpd.
After we stopped httpd, the error disappeared.
Then we type "localhost" in a web browser, we got the following words printed:
nginx, configured by Ansible
If you can see this, Ansible successfully installed nginx.
Ansible managed
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단! Certbot을 사용하여 웹 사이트를 SSL(HTTPS)화하는 방법초보자가 인프라 주위를 정돈하는 것은 매우 어렵습니다. 이번은 사이트를 간단하게 SSL화(HTTP에서 HTTPS통신)로 변경하는 방법을 소개합니다! 이번에는 소프트웨어 시스템 Nginx CentOS7 의 환경에서 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.