Showing posts with label nginx. Show all posts
Showing posts with label nginx. Show all posts

Thursday, May 23, 2024

Prometheus and Grafana : Monitor MySQL/MariaDB

 1.Install MySQL Exporter
#curl -s https://api.github.com/repos/prometheus/mysqld_exporter/releases/latest   | grep browser_download_url   | grep linux-amd64 | cut -d '"' -f 4   | wget -qi -
#tar xvf mysqld_exporter*.tar.gz
#sudo mv  mysqld_exporter-*.linux-amd64/mysqld_exporter /usr/local/bin/
#sudo chmod +x /usr/local/bin/mysqld_exporter

2.Checking version of mysql exporter

#mysqld_exporter  --version

3.Create a user to collect the matrices of MySQL

#mysql -u root -p

CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost';
FLUSH PRIVILEGES;
EXIT

4.Create database credentials file
#sudo vi /etc/.mysqld_exporter.cnf
[client]
user=mysqld_exporter
password=StrongPassword

5.Enable ownership permission
#chown root:prometheus /etc/.mysqld_exporter.cnf

6.Create a service for mysql exporter
#vi /etc/systemd/system/mysql_exporter.servic
[Unit]
Description=Prometheus MySQL Exporter
After=network.target
User=prometheus
Group=prometheus

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/mysqld_exporter \
--config.my-cnf /etc/.mysqld_exporter.cnf \
--collect.global_status \
--collect.info_schema.innodb_metrics \
--collect.auto_increment.columns \
--collect.info_schema.processlist \
--collect.binlog_size \
--collect.info_schema.tablestats \
--collect.global_variables \
--collect.info_schema.query_response_time \
--collect.info_schema.userstats \
--collect.info_schema.tables \
--collect.perf_schema.tablelocks \
--collect.perf_schema.file_events \
--collect.perf_schema.eventswaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tableiowaits \
--collect.slave_status \
--web.listen-address=0.0.0.0:9104

[Install]
WantedBy=multi-user.target

7.Enable and start the mysql exporter service
#systemctl daemon-reload
#systemctl enable mysql_exporter
#systemctl start mysql_exporter
#systemctl status mysql_exporter

8.Configure the endpoint in “Prometheus.yaml” file
# mysql exporter
  - job_name: "mysqld"
    metrics_path: '/metrics'
    scheme: http
    static_configs:
      - targets: ["X.X.X.X:9104"]

9.Import dashboard from Grafana.com with ID:7362


source : https://shrihariharidas73.medium.com/unlocking-database-insights-monitoring-mysql-with-prometheus-and-grafana-ddd2c4f01929

Tuesday, May 21, 2024

Prometheus and Grafana : Monitor NGINX

A: nginx configuration

1.Expose the stub_status page at /stub_status on port 8080

    server {
        listen 8080;
        server_name _;
    
        location /stub_status {
               stub_status;
        }
    }
2. Restart nginx
#systemctl restart nginx

3. Verification
#curl http://localhost:8080/stub_status

B: nginx-prometheus-exporter

1.Install nginx-prometheus-exporter
#git clone https://github.com/nginxinc/nginx-prometheus-exporter.git

2.Building the Binary. Change to nginx-prometheus-exporter folder
#dnf install go
#make

3.Move the nginx-prometheus-exporter binary to /usr/local/binary
#mv nginx-prometheus-exporter /usr/local/bin

4.Modify and create nginx_exporter.socket in /etc/systemd/system
#vi nginx_exporter.socket  (add below parameter)

[Unit]
Description=NGINX Prometheus Exporter

[Socket]
ListenStream=9113

[Install]
WantedBy=sockets.target

4.Modify and create nginx_exporter.service in /etc/systemd/system
#useradd nginx_exporter --shell=/sbin/nologin
#vi nginx_exporter.service  (add below parameter)
[Unit]
Description=NGINX Prometheus Exporter
Requires=nginx_exporter.socket

[Service]
User=nginx_exporter
ExecStart=/usr/local/bin/nginx-prometheus-exporter --nginx.scrape-uri="http://127.0.0.1:8080/stub_status" --web.systemd-socket

[Install]
WantedBy=multi-user.target

5.Run #systemctl daemon-reload
6.Run #systemctl start nginx_prometheus_exporter.service
7.Run #systemctl status nginx_prometheus_exporter.service
8. Verification
#curl http://localhost:9113/metrics

C:Promethues

1.Configure Prometheus to scrape metrics from the server with the exporter.
2.Edit /etc/prometheus/prometheus.yml
3.Add the scrape metric
## NGINX
  - job_name: nginx
    static_configs:
      - targets: ['10.X.X.X:9113']

4.Run systemctl restart prometheus.service
5.Run systemctl status prometheus.service

D:Grafana

1.Use the New Dashboard button and click Import.
2.Upload dashboard.json or copy and paste the contents of the file in the textbox and click Load.
https://github.com/nginxinc/nginx-prometheus-exporter/blob/main/grafana/dashboard.json
3.Set the Prometheus data source and click Import.
4.The dashboard will appear.By default, all instances are selected
5.Access Grafana dashboard

source : https://github.com/nginxinc/nginx-prometheus-exporter/tree/main

Wednesday, April 24, 2024

Nginx : Deploy Laravel

1.Install Laravel

2.Edit parament in Nginx configuration

server {
        server_name example.com;
        listen X.X.X.X;
        root /home/example/public_html/laravel/public; <- change the active laravel folder
        index index.php index.htm index.html;
        access_log /var/log/virtualmin/example.com_access_log;
        error_log /var/log/virtualmin/example.com_error_log;

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
       
        error_page 404 /index.php;
       
        location / {
                       index index.php index.html index.htm;
                       try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ "\.php(/|$)" {
            try_files $uri $fastcgi_script_name =404;
            default_type application/x-httpd-php;
            fastcgi_pass unix:/run/php-fpm/1713861882126181.sock;
        }

        fastcgi_split_path_info "^(.+\.php)(/.+)$";
        if ($host = example.com) {
            rewrite "^/(.*)$" "https://example.com/$1" redirect;
        }

        # SSL Certiticate
        listen X.X.X.X:443 ssl;
        ssl_certificate /home/example/ssl.cert;
        ssl_certificate_key /home/example/ssl.key;
        rewrite /awstats/awstats.pl /cgi-bin/awstats.pl;
    }
}

3.Test nginx configuration

#nginx -t 

4. Restart nginx services

#systemctl restart nginx