skip to Main Content

We have a lot of servers and vhosts and some of them use nignx, some httpd or apache for their domains. I want to write an ansible script that pulls the information out on what web server the vhost uses for its site, the problem is that a lot of them have all 3 web servers installed but only one is actually used for the site to run (others might have an active status).

I mean, I don’t even know where to begin. A simple example of a bash script would be nice that accurately outputs the web server of the vhost.

2

Answers


  1. how about checking the ports 80 or 443 which process is using them:

    sudo netstat -ntlp | grep '0.0.0.0:80' | awk '{print $7}'
    

    and for port 443:

    sudo netstat -ntlp | grep '0.0.0.0:443' | awk '{print $7}'
    

    The output will be one of those lines:

    xx/httpd # for apache
    xx/nginx # for nginx
    

    And you can write an ansible task to run this cmd

    Login or Signup to reply.
  2. Q: “If a server has nginx and apache installed, how do I find out which one is used for the website?

    A: Given a “website” we know the IP address and port. Then the question is: What daemon is running at www_ip host and www_port?

    Let’s use lsof and print the first item on the list. The play below (as an example nginx is listening at port 8080)

    - hosts: www_ip
      vars:
        www_port: 8080
      tasks:
        - shell: "lsof -i :{{ www_port }}|
                  grep LISTEN|
                  cut -d ' ' -f 1"
          register: result
        - debug:
            msg: "{{ result.stdout_lines|first }}"
    

    gives

    ok: [www_ip] => {
        "msg": "nginx"
    }
    

    Notes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search