Tutorials | Computing

Installing GoAccess with Nginx or Apache Using Docker

942 words 5 mins read > 0 comments 674 views

    Installing GoAccess with Nginx or Apache Using Docker

    Introduction

    GoAccess is a real-time web log analyzer and interactive viewer that runs in a terminal on Unix-like systems or through your browser. It provides fast and valuable HTTP statistics for system administrators, offering detailed reports on metrics like visitor locations, operating systems, bandwidth usage, and more.

    https://res.cloudinary.com/diupzzxbt/images/f_auto,q_auto/v1690318391/Goaccess/Goaccess.png?_i=AA

    Nginx Admin (also referred to as Nginx Manager) is a tool for managing Nginx servers. It simplifies the configuration and monitoring of Nginx, making it easier to handle web traffic, manage server resources, and ensure high performance and availability of web applications.

    In this guide, we will cover how to install and run GoAccess with either Nginx or Apache using Docker. This setup will allow you to monitor your web server logs in real-time through a web interface, providing valuable insights into your web traffic.

    Prerequisites

    Before we start, ensure you have the following:

    1. A server running a Unix-based OS (e.g., Ubuntu, Debian).
    2. Docker and Docker Compose installed on your server.
    3. Nginx or Apache installed and running on your server.

    Step 1: Set Up Nginx or Apache

    You can use either Nginx or Apache to serve your web traffic. Follow the appropriate instructions based on your choice.

    Nginx

    If you choose Nginx, follow these steps:

    • Install Nginx:
    sudo apt update
    sudo apt install nginx
    
    • Configure Nginx to Log Access Logs:

    Ensure the access_log directive is set correctly in your Nginx configuration file (usually found in /etc/nginx/nginx.conf or /etc/nginx/sites-available/default):

    access_log /var/log/nginx/access.log;
    

    Ensure the access_log directive is set correctly in your Nginx configuration file (usually found in /etc/nginx/nginx.conf or /etc/nginx/sites-available/default):

    • Restart Nginx:
    sudo systemctl restart nginx

    Apache

    If you choose Apache, follow these steps:

    • Install Apache:

    sudo apt update
    sudo apt install apache2
    
    • Configure Apache to Log Access Logs:

    Ensure the LogFormat and CustomLog directives are set correctly in your Apache configuration file (usually found in /etc/apache2/apache2.conf or /etc/apache2/sites-available/000-default.conf):

    LogFormat "%h %l %u %t \"%r\" %>s %b" combined
    CustomLog /var/log/apache2/access.log combined
    
    • Restart Apache:
    sudo systemctl restart apache2
    

    Step 2: Install Docker and Docker Compose

    If you haven't installed Docker and Docker Compose yet, you can do so with the following commands:

    Install Docker:

    sudo apt update
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    sudo apt update
    sudo apt install -y docker-ce
    

    Install Docker Compose:

    sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose --version
    

    Step 3: Create Docker Compose File

    Create a new directory for your project and navigate into it:

    mkdir goaccess-docker
    cd goaccess-docker
    

    Create a docker-compose.yml file with the following content:

    version: '3'
    
    services:
      webserver:
        image: nginx:latest  # Replace with 'httpd:latest' for Apache
        ports:
          - "80:80"
        volumes:
          - ./webserver.conf:/etc/nginx/nginx.conf  # Replace with 'apache.conf' for Apache
          - ./log:/var/log/nginx  # Replace with '/var/log/apache2' for Apache
    
      goaccess:
        image: allinurl/goaccess
        volumes:
          - ./log:/usr/local/nginx/logs  # Replace with '/usr/local/apache2/logs' for Apache
          - ./goaccess:/goaccess
        command: goaccess /usr/local/nginx/logs/access.log -o /goaccess/report.html --log-format=COMBINED --real-time-html
        ports:
          - "7880:7880"
    

    Step 4: Create Web Server Configuration File

    In the same directory, create a configuration file for your chosen web server.

    Nginx

    Create a webserver.conf file with the following content:

    events {}
    
    http {
        log_format combined '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
        
        access_log /var/log/nginx/access.log combined;
    
        server {
            listen 80;
    
            location / {
                root /usr/share/nginx/html;
                index index.html index.htm;
            }
        }
    }
    

    Apache

    Create an apache.conf file with the following content:

    ServerRoot "/usr/local/apache2"
    Listen 80
    
    LoadModule mpm_event_module modules/mod_mpm_event.so
    LoadModule authn_core_module modules/mod_authn_core.so
    LoadModule authz_core_module modules/mod_authz_core.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule unixd_module modules/mod_unixd.so
    LoadModule dir_module modules/mod_dir.so
    LoadModule alias_module modules/mod_alias.so
    
    User daemon
    Group daemon
    
    ServerAdmin you@example.com
    ServerName localhost
    
    DocumentRoot "/usr/local/apache2/htdocs"
    <Directory />
        AllowOverride none
        Require all denied
    </Directory>
    
    <Directory "/usr/local/apache2/htdocs">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    
    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
    
    LogFormat "%h %l %u %t \"%r\" %>s %b" combined
    CustomLog /var/log/apache2/access.log combined
    
    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    

    Step 5: Create Required Directories

    Create the necessary directories for logs and GoAccess data:

    mkdir log
    mkdir goaccess
    

    Step 6: Run Docker Compose

    Now, start your Docker containers:

    docker-compose up -d
    

    This command will start both the web server and GoAccess containers in the background. The web server will serve your web traffic and log the access logs to the log directory. GoAccess will read these logs and generate a real-time HTML report.

    Step 7: Access GoAccess Report

    Open your browser and navigate to http://your_server_ip:7880 to see the GoAccess real-time report.

    Conclusion

    You've now successfully set up GoAccess with either Nginx or Apache using Docker. This setup allows you to monitor your web server logs in real-time through a web interface, providing valuable insights into your web traffic. Adjust configurations and explore further customization to suit your needs.

    Comments


    No Comments
    POST COMMENT
    captcha
    Back