<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shubham Patil</title>
    <description>The latest articles on DEV Community by Shubham Patil (@shub6059).</description>
    <link>https://dev.to/shub6059</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F547286%2Ff2f78aa3-680e-4059-801f-b060140c4545.png</url>
      <title>DEV Community: Shubham Patil</title>
      <link>https://dev.to/shub6059</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shub6059"/>
    <language>en</language>
    <item>
      <title>Install and Setup Grafana &amp; Prometheus on Ubuntu 20.04 | 22.04/EC2</title>
      <dc:creator>Shubham Patil</dc:creator>
      <pubDate>Fri, 15 Mar 2024 06:27:15 +0000</pubDate>
      <link>https://dev.to/shub6059/install-and-setup-grafana-prometheus-on-ubuntu-2004-2204ec2-3i62</link>
      <guid>https://dev.to/shub6059/install-and-setup-grafana-prometheus-on-ubuntu-2004-2204ec2-3i62</guid>
      <description>&lt;p&gt;What is Prometheus ?&lt;br&gt;
Prometheus is a open source Linux Server Monitoring tool mainly used for metrics monitoring, event monitoring, alert management, etc.&lt;br&gt;
Prometheus has changed the way of monitoring systems and that is why it has become the Top-Level project of Cloud Native Computing Foundation (CNCF).&lt;br&gt;
Prometheus uses a powerful query language i.e. “PromQL”.&lt;br&gt;
In Prometheus tabs are on and handles hundreds of services and microservices.&lt;br&gt;
Prometheus use multiple modes used for graphing and dashboarding support.&lt;br&gt;
Prometheus Components&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prometheus Server
Prometheus server is a first component of Prometheus architecture.
Prometheus server is a core of Prometheus architecture which is divided into several parts like Storage, PromQL, HTTP server, etc.
In Prometheus server data is scraped from the target nodes and then stored int the database.
1.a. Storage
Storage in Prometheus server has a local on disk storge.
Prometheus has many interfaces that allow integrating with remote storage systems.
1.b. PromQL&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Prometheus uses its own query language i.e. PromQL which is very powerful querying language.&lt;br&gt;
PromQL allows the user to select and aggregate the data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Service Discovery
Next and very important component of Prometheus Server is the Service Discovery.
With the help of Service discovery the services are identified which are need to scraped.
To Pull metrics, identification of services and finding the targets are compulsory needed.
Through Service discovery we monitor the entities and can also locate its targets.&lt;/li&gt;
&lt;li&gt;Scrape Target
Once the services are identified and the targets are ready then we can pull metrics from it and can scrape the target.
We can export the data of end point using node exporters.
Once the metrics or other data is pulled, Prometheus stores it in a local storage.&lt;/li&gt;
&lt;li&gt;Alert Manager
Alert Manager handles the alerts which may occurs during the session.
Alert manager handles all the alerts which are sent by Prometheus server.
Alert manager is one of the very useful component of Prometheus tool.
If in case any big error or any issue occurs, alert manager manage those alerts and contact with human via E-mail, Text Messages, On-call, or any other chat application service.&lt;/li&gt;
&lt;li&gt;User Interface
User interface is also a important component as it builds a bridge between the user and the system.
In Prometheus, user interface are note that much user friendly and can be used till graph queries.
For good exclusive dashboards Prometheus works together with Grafana (visualization tool).
Using Grafana over Prometheus to visualize properly we can use custom dashboards.
Grafana dashboards displays via pie charts, line charts, tables, good data graphs of CPU usage, RAM utilization, network load, etc with indicators.
Grafana supports and run with Prometheus by querying language i.e. PromQL.
To fetch data from Prometheus and to display the results on Grafana dashboards PromQL is used.
What is Grafana ?
Grafana is a free and open source visualization tool mostly used with Prometheus to which monitor metrics.
Grafana provides various dashboards, charts, graphs, alerts for the particular data source.
Grafana allows us to query, visualize, explore metrics and set alerts for the data source which can be a system, server, nodes, cluster, etc.
We can also create our own dynamic dashboard for visualization and monitoring.
We can save the dashboard and can even share with our team members which is one of the main advantage of Grafana.
What is Node Exporter ?
Node exporter is one of the Prometheus exporters which is used to expose servers or system OS metrics.
With the help of Node exporter we can expose various resources of the system like RAM, CPU utilization, Memory Utilization, disk space.
Node exporter runs as a system service which gathers the metrics of your system and that gathered metrics is displayed with the help of Grafana visualization tool.
Prerequisites
Ubuntu with 22.04 Version
Root user account with sudo privilege.
Prometheus system user and group.
Sufficient storage on your system and good internet connectivity.
Ports Required- 9090 (Prometheus), 3000 (Grafana), 9100 (Node Exporter)
We will update the system repository index by using the following command.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step #1:Creating Prometheus System Users and Directory&lt;br&gt;
Create a system user for Prometheus&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo useradd --no-create-home --shell /bin/false prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Create the directories in which we will be storing our configuration files and libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Set the ownership of the /var/lib/prometheus directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo chown prometheus:prometheus /var/lib/prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Step #2:Download Prometheus Binary File&lt;br&gt;
Now we will download the latest version of Prometheus. We can copy the download link as per our Operating System from Prometheus download page&lt;/p&gt;

&lt;p&gt;Using below command we can download Prometheus, here we are downloading Prometheus 2.46 version, you use above link to download specific version.&lt;/p&gt;

&lt;p&gt;You need to inside /tmp :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /tmp/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Download the Prometheus setup using wget&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wget https://github.com/prometheus/prometheus/releases/download/v2.46.0/prometheus-2.46.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Extract the files using tar&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xvf prometheus-2.46.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Move the configuration file and set the owner to the prometheus user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd prometheus-2.46.0.linux-amd64

sudo mv console* /etc/prometheus

sudo mv prometheus.yml /etc/prometheus

sudo chown -R prometheus:prometheus /etc/prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Move the binaries and set the owner&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo mv prometheus /usr/local/bin/

sudo chown prometheus:prometheus /usr/local/bin/prometheus

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Step #3:Prometheus configuration file&lt;br&gt;
We have already copied /opt/prometheus-2.26.0.linux-amd64/prometheus.yml file /etc/prometheus directory, verify if it present and should look like below and modify it as per your requirement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
sudo nano /etc/prometheus/prometheus.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step #4:Creating Prometheus Systemd file&lt;br&gt;
Create the service file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
[Unit]

Description=Prometheus

Wants=network-online.target

After=network-online.target

[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/prometheus \

— config.file /etc/prometheus/prometheus.yml \

— storage.tsdb.path /var/lib/prometheus/ \

— web.console.templates=/etc/prometheus/consoles \

— web.console.libraries=/etc/prometheus/console_libraries

[Install]

WantedBy=multi-user.target

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;/p&gt;

&lt;p&gt;Reload systemd&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start, Enable, Status Prometheus service&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start prometheus

sudo systemctl enable prometheus

sudo systemctl status prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Launch the Prometheus server by executing the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
./prometheus –config.file=prometheus.yml (or) ./prometheus
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Prometheus Installation:&lt;/p&gt;

&lt;p&gt;Open your web browser and access &lt;a href="http://server-ip-address:9090"&gt;http://server-ip-address:9090&lt;/a&gt;. If Prometheus is running correctly, you will see the Prometheus web interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3igq6nwpgmdbs78mruw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi3igq6nwpgmdbs78mruw.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac2dnjih0vctt9uq3sr6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac2dnjih0vctt9uq3sr6.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpv7viyjeq0j0i67bd6lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpv7viyjeq0j0i67bd6lc.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3a8hfz0upt4p0nh20jc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd3a8hfz0upt4p0nh20jc.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Steps to Install and set up the Grafana on Ubuntu 20.04 and 22.04/EC2&lt;/p&gt;

&lt;p&gt;Grafana&lt;br&gt;
What is Grafana?&lt;br&gt;
Grafana is an open-source platform for data visualization, monitoring, and analysis. It allows you to create and display interactive, customizable dashboards with real-time monitoring and analytics capabilities. Grafana supports a wide range of data sources, including time-series databases and relational databases.&lt;br&gt;
With Grafana, you can easily create visualizations and alerts based on your data, as well as collaborate with other team members through sharing and annotations. It also provides advanced features like plugins, annotations, and panels, which allow you to extend the platform’s capabilities and create custom visualizations and integrations.&lt;br&gt;
Grafana is widely used in industries such as IT, finance, healthcare, and more, for monitoring and analyzing various metrics, logs, and sensor data. It is known for its user-friendly interface, ease of use, and powerful features, making it a popular choice for data visualization and monitoring.&lt;br&gt;
Installation of Grafana&lt;br&gt;
Update your Ubuntu system using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting packages on Ubuntu distributions&lt;br&gt;
Add the Grafana repository to your Ubuntu installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Grafana as a service&lt;br&gt;
Now that all the packages are available, it is time for you to install them.&lt;br&gt;
Update your package list using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the Grafana package using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install grafana
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check Grafana version&lt;br&gt;
You can use the Grafana version command to check the currently installed version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grafana-server -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the Grafana service using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start grafana-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable the Grafana service to start on boot:&lt;br&gt;
To start it and make sure that the service is always available every time the machine is restarted, type the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable grafana-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify your Grafana installation&lt;br&gt;
Check the status of the Grafana service to make sure it is running.&lt;br&gt;
To verify it, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status grafana-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Grafana should run automatically, but if this is not the case, make sure to start it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo service grafana-server start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the future to stop and restart the Grafana, the commands are:&lt;br&gt;
Restart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo service grafana-server restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo service grafana-server stop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow Grafana TCP port 3000 in the Firewall&lt;br&gt;
Grafana's default HTTP port is 3000, you’ll need to allow access to this port on the firewall.&lt;br&gt;
If your firewall is UFW type the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow 3000/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fox1c46nb6nepjcl7ekfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fox1c46nb6nepjcl7ekfj.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it! You have now installed Grafana on Ubuntu. You can now start using it to create dashboards and visualize your data.&lt;br&gt;
Set up Grafana&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Access the Grafana web UI by visiting &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; in your web browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ysd2kbxe14gv2mszcs2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ysd2kbxe14gv2mszcs2.png" alt="Image description" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;   Login to Grafana web UI
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2utady3g7mlow8gl0hy0.png" alt="Image description" width="800" height="405"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Dashboard Grafana &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfid5b5kqogsplye55yd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfid5b5kqogsplye55yd.png" alt="Image description" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Setup for Data Source &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7jssz321wvqhba2zitq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7jssz321wvqhba2zitq.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F104ar974g8kk5aegptct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F104ar974g8kk5aegptct.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7eyjxe8zn3h5yk0z78ge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7eyjxe8zn3h5yk0z78ge.png" alt="Image description" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18uqi50b617r4ue39yij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F18uqi50b617r4ue39yij.png" alt="Image description" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl1ubswnfefh3e3sqzzxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl1ubswnfefh3e3sqzzxn.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffywblki9sc3ni9hbmrhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffywblki9sc3ni9hbmrhy.png" alt="Image description" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After Setup Final Dashboard Output:=&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficwieqxcu98crscd12fv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficwieqxcu98crscd12fv.png" alt="Image description" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>prometheus</category>
      <category>webdev</category>
      <category>aws</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Containerizing PHP,Apache, and MySQL</title>
      <dc:creator>Shubham Patil</dc:creator>
      <pubDate>Tue, 27 Feb 2024 12:57:55 +0000</pubDate>
      <link>https://dev.to/shub6059/containerizing-phpapache-and-mysql-3oan</link>
      <guid>https://dev.to/shub6059/containerizing-phpapache-and-mysql-3oan</guid>
      <description>&lt;p&gt;Introduction&lt;br&gt;
Containerizing a PHP, Apache, and MySQL stack for hosting a blog is a great way to ensure consistency across different environments and simplify deployment processes. Docker is a popular tool for containerization that can help achieve this. Here's a step-by-step guide to containerizing such a stack:&lt;/p&gt;

&lt;p&gt;The project structure is as follows :&lt;/p&gt;

&lt;p&gt;/php-apache-mysql/&lt;br&gt;
├── apache&lt;br&gt;
│ ├── apache_php.conf&lt;br&gt;
│ └── Dockerfile&lt;br&gt;
├── Cache&lt;br&gt;
│ ├── cbc42e4c979f69f0_0&lt;br&gt;
│ └── index-dir&lt;br&gt;
│ └── the-real-index&lt;br&gt;
├── docker-compose.yml&lt;br&gt;
├── Network Persistent State&lt;br&gt;
├── php&lt;br&gt;
│ └── Dockerfile&lt;br&gt;
├── public&lt;br&gt;
│ ├── config.php&lt;br&gt;
│ ├── create.php&lt;br&gt;
│ ├── delete.php&lt;br&gt;
│ ├── dump&lt;br&gt;
│ │ └── dump.sql&lt;br&gt;
│ ├── error.php&lt;br&gt;
│ ├── index.php&lt;br&gt;
│ ├── read.php&lt;br&gt;
│ └── update.php&lt;br&gt;
└── README.md&lt;/p&gt;

&lt;p&gt;Once this structure is replicated or cloned(using "git clone &lt;a href="https://github.com/shub6059/php_apache_docker.git%22"&gt;https://github.com/shub6059/php_apache_docker.git"&lt;/a&gt;) with these files, Docker and Docker compose is installed locally, you can simply run "docker-compose up" from the root of the project to run this project, and point your browser to &lt;a href="http://localhost:8080"&gt;http://localhost:8080&lt;/a&gt; to see the project running.&lt;br&gt;
Docker Compose&lt;/p&gt;

&lt;p&gt;Docker compose allows us to define the dependencies for the services, networks, volumes, etc as code.&lt;br&gt;
docker-compose.yml&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
&lt;code&gt;version: "3"&lt;br&gt;
services:&lt;br&gt;
 php:&lt;br&gt;
 build: './php/'&lt;br&gt;
 volumes:&lt;br&gt;
 - ./public:/var/www/html/&lt;br&gt;
 apache:&lt;br&gt;
 build: './apache/'&lt;br&gt;
 depends_on:&lt;br&gt;
 - php&lt;br&gt;
 - mysql&lt;br&gt;
 ports:&lt;br&gt;
 - "8080:80"&lt;br&gt;
 volumes:&lt;br&gt;
 - ./public:/var/www/html/&lt;br&gt;
 mysql:&lt;br&gt;
 image: mysql:5.7&lt;br&gt;
 restart: always&lt;br&gt;
 ports:&lt;br&gt;
 - "3306:3306"&lt;br&gt;
 volumes:&lt;br&gt;
 - db_data:/var/lib/mysql&lt;br&gt;
 # Initially creates employees table&lt;br&gt;
 - ./public/dump:/docker-entrypoint-initdb.d/&lt;br&gt;
 environment:&lt;br&gt;
 MYSQL_ROOT_PASSWORD: "passwd"&lt;br&gt;
 MYSQL_DATABASE: "mydb"&lt;br&gt;
 MYSQL_USER: "user1"&lt;br&gt;
 MYSQL_PASSWORD: "passwd"&lt;br&gt;
volumes:&lt;br&gt;
 db_data:&lt;/code&gt;&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
Here we are decoupling Apache, PHP and Mysql by building them out into separate containers.We will be using the below Dockerfiles to decouple them :&lt;br&gt;
apache/Dockerfile&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -&lt;br&gt;
&lt;code&gt;FROM httpd:2.4.33-alpine&lt;br&gt;
RUN apk update; \&lt;br&gt;
 apk upgrade;&lt;br&gt;
COPY apache_php.conf /usr/local/apache2/conf/apache_php.conf&lt;br&gt;
RUN echo "Include /usr/local/apache2/conf/apache_php.conf" \&lt;br&gt;
 &amp;gt;&amp;gt; /usr/local/apache2/conf/httpd.conf&lt;/code&gt;&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
php/Dockerfile &lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
&lt;code&gt;FROM php:7.2.7-fpm-alpine3.7&lt;br&gt;
RUN apk update; \&lt;br&gt;
 apk upgrade;&lt;br&gt;
RUN docker-php-ext-install mysqli&lt;/code&gt;&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
Networking&lt;/p&gt;

&lt;p&gt;We're going to have Apache proxy connections which require PHP rendering to port 9000 of our PHP container, and then have the PHP container serve those out as rendered HTML.&lt;br&gt;
We need an apache vhost configuration file that is set up to proxy these requests for PHP files to the PHP container. So in the Dockerfile for Apache we have defined above, we add this file and then include it in the base httpd.conf file. This is for the proxying which allows us to decouple Apache and PHP. Here we called it apache_php.conf and we have the proxying modules defined as well as the VirtualHost.&lt;br&gt;
apache/apache_php.conf&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
&lt;code&gt;ServerName localhost&lt;br&gt;
LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so&lt;br&gt;
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so&lt;br&gt;
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so&lt;br&gt;
&amp;lt;VirtualHost *:80&amp;gt;&lt;br&gt;
 # Proxy .php requests to port 9000 of the php-fpm container&lt;br&gt;
 ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1&lt;br&gt;
 DocumentRoot /var/www/html/&lt;br&gt;
 &amp;lt;Directory /var/www/html/&amp;gt;&lt;br&gt;
 DirectoryIndex index.php&lt;br&gt;
 Options Indexes FollowSymLinks&lt;br&gt;
 AllowOverride All&lt;br&gt;
 Require all granted&lt;br&gt;
 &amp;lt;/Directory&amp;gt;&lt;br&gt;
 &lt;br&gt;
 # Send apache logs to stdout and stderr&lt;br&gt;
 CustomLog /proc/self/fd/1 common&lt;br&gt;
 ErrorLog /proc/self/fd/2&lt;br&gt;
&amp;lt;/VirtualHost&amp;gt;&lt;/code&gt;&lt;br&gt;
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &lt;br&gt;
Volumes&lt;/p&gt;

&lt;p&gt;Both the PHP and Apache containers have access to a "volume" that we define in the docker-compose.yml file which maps the public folder of our repository to the respective services for them to access. When we do this, we map a folder on the host filesystem (outside of the container context) to inside of the running containers.This allows us to edit the file outside of the container, yet have the container serve the updated PHP code out as soon as changes are saved.&lt;br&gt;
And we also use "db_data" volume to store the contents of database, so that even if containers are terminated the data won't get deleted.&lt;br&gt;
PHP CRUD application&lt;/p&gt;

&lt;p&gt;We'll use the following PHP application to demonstrate everything:&lt;br&gt;
"dump.sql" creates a table 'employees' in the mydb database.&lt;br&gt;
"config.php" connects the php code with the 'employees' table of mydb database.&lt;br&gt;
"index.php" creates the frontend grid which displays records from "employees" table.&lt;br&gt;
"create.php" generates web form that is used to insert records in the employees table.&lt;br&gt;
"read.php" retrieves the records from the employees table.&lt;br&gt;
"update.php" updates records in employees table.&lt;br&gt;
"delete.php" deletes records in employees table.&lt;br&gt;
"error.php" will be displyed if a request is invalid.&lt;br&gt;
STEP :- Clone repo &lt;br&gt;
git clone &lt;a href="https://github.com/shub6059/php_apache_docker.git"&gt;https://github.com/shub6059/php_apache_docker.git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdajh4h85qhy37v4q357.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxdajh4h85qhy37v4q357.png" alt="Image description" width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open in VS Code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falxvur2yllb72lx1gdal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Falxvur2yllb72lx1gdal.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this type open clone repo structure&lt;br&gt;
Start create Container&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdanka9j56764ckp8cqcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdanka9j56764ckp8cqcf.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;get data in Browser&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futhcy32b9uopslqqx78k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futhcy32b9uopslqqx78k.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open url see output &lt;br&gt;
&lt;a href="http://localhost:8080/"&gt;http://localhost:8080/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz72pq1kpm53pb88hffcm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz72pq1kpm53pb88hffcm.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fggny4qy0by7u3uqaqo9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fggny4qy0by7u3uqaqo9n.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac34h5zps51ao2we52r2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fac34h5zps51ao2we52r2.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>aws</category>
      <category>devops</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
