Looking to deploy your website with ease? Look no further! In this tutorial, I’ll guide you through the process of containerizing your website using Docker. Don’t worry if you’re new to Docker — I’ll break it down into simple steps.
Step 1: Pull the Nginx Alpine Image
Let’s start by pulling the lightweight Nginx Alpine image from the Docker Hub. This image will serve as the web server for our website. Open your terminal and run the following command:
docker image pull nginx:alpine
Step 2: Setting Up the Environment
Next, let’s create the necessary environment for our website deployment. We’ll begin by copying the default Nginx configuration file from the container to our local directory. This file contains the server configuration and can be customized to fit our needs. Run the following command:
docker container cp nginx:/etc/nginx/conf.d/default.conf .
Open the default.conf
file and customize it according to your website requirements. This file defines how Nginx will handle incoming requests and serve the website files. Below is an example configuration:
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
index index.php index.html;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
In this configuration, Nginx acts as the communication layer with clients. When a client requests a .php
extension file, Nginx identifies the file’s location from the document root and forwards the location to the PHP-FPM container. The PHP-FPM container then processes the file and provides the processed output to the Nginx container, which forwards it to the client.
Step 3: Cloning the Website Files
Now, let’s clone our website into a directory named “website/”. You can replace this with your own website files. Make sure the website files are compatible with Nginx and PHP-FPM.
Step 4: Creating the Network
To ensure seamless communication between containers, we’ll create a bridge network named “php-app-net”. This network will allow the Nginx container to communicate with the PHP-FPM container. Execute the following command:
docker network create php-app-net
Step 5: Launching the PHP-FPM Container
Now, let’s create the PHP-FPM container, which will handle the PHP code execution. Run the following command:
docker container run -d --name php-fpm --restart always --network php-app-net -v ${pwd}/website/:/usr/share/nginx/html php:fpm
Step 6: Launching the Nginx Container
Lastly, we’ll launch the Nginx container to serve our website to the outside world. Run the following command:
docker container run -d --name nginx --restart always --network php-app-net -p 80:80 -v $(pwd)/website/:/usr/share/nginx/html -v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf nginx:alpine
Step 7: Verifying the Deployment ✔️
To ensure everything is working smoothly, execute the following command to check the running containers:
docker container ls -a
You should see two containers listed: nginx
and php-fpm
.
Step 8: Testing the Website
Congratulations! Your website is up and running. Open your browser and navigate to http://localhost:80
to access the website. You should see your website displayed correctly.
Step 9: Scaling and Extending
Docker allows easy scaling and extension of your deployment. Need more PHP-FPM containers to handle increased traffic? Simply launch additional containers and connect them to the php-app-net
network.
Launch additional PHP-FPM containers using the docker container run
command. Make sure to provide a unique name for each container and connect them to the existing php-app-net
network. Here’s an example command:
docker container run -d --name php-fpm2 --restart always --network php-app-net -v $(pwd)/website/:/usr/share/nginx/html php:fpm
You need to update the default.conf
file with the appropriate configuration.
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
index index.php index.html;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
# Forward requests to the first PHP-FPM container
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
# Forward requests to the second PHP-FPM container (php-fpm2)
fastcgi_pass php-fpm2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
In the updated configuration, we added a new location
block to handle requests to PHP files and forward them to the second PHP-FPM container (php-fpm2
) on port 9000
. This allows Nginx to distribute the workload across multiple PHP-FPM containers.
Remember to restart the Nginx container after making changes to the default.conf
file for the new configuration to take effect.
By updating the default.conf
file as mentioned above, Nginx will be able to communicate with both PHP-FPM containers and distribute the requests accordingly.
Conclusion
You’ve successfully containerized your website using Docker! By leveraging Nginx and PHP-FPM in separate containers, you’ve achieved a scalable and efficient setup. Docker simplifies the deployment process and ensures consistent environments across different systems.