Introduction
In this article, we will discuss how to set up Coolify environment with Docker Compose, Nginx Proxy, and two domains. Nginx Proxy will serve as a static file server and act as an reverse proxy for the PHP server. This setup will help us to handle timeouts for our PHP applications and improve overall performance.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- Docker installed on your system
- Docker Compose installed on your system
- Basic understanding of Docker and Docker Compose
- Basic understanding of Nginx and Nginx Proxy
Docker Compose Setup
First, let's create a Docker Compose file to define our services. Create a new file named "docker-compose.yml" and add the following content:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./certs:/etc/nginx/certs
- ./html:/usr/share/nginx/html
- ./logs:/var/log/nginx
- ./php:/var/www/html
- ./php-fpm.conf:/etc/php/php-fpm.conf:ro
- ./php.ini:/usr/local/etc/php/php.ini:ro
php:
image: php:7.4-fpm
container_name: php
volumes:
- ./php:/var/www/html
Nginx Configuration
Now, let's create the Nginx configuration file. Create a new file named "nginx.conf" and add the following content:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name domain1.com;
return 301 https://$server_name$request_uri;
}
server {
listen 80;
server_name domain2.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name domain1.com;
ssl_certificate /etc/nginx/certs/domain1.com.crt;
ssl_certificate_key /etc/nginx/certs/domain1.com.key;
location / {
proxy_pass http://php:9000;
proxy_set_status_zone php_status;
proxy_next_upstream error timeout invalid_header http_500 http_501 http_502 http_503 http_504;
proxy_next_upstream_tries 1;
proxy_next_upstream_timeout 120;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /static {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
PHP Configuration
Now, let's configure PHP. Add the following content to the "php.ini" file:
cgi.fix_pathinfo=0
cgi.force_redirect = 0
cgi.redirect_status_code = 404
Certificates
Generate and install SSL certificates for both domains using Let's Encrypt or any other certificate authority. Place the certificates in the "/etc/nginx/certs" directory.
Building and Running the Application
Build and run the application using the following command:
docker-compose up -d
Testing
Visit both domains in your browser, and you should see your PHP applications being served through Nginx Proxy with proper timeouts and error handling.
Conclusion
In this article, we discussed how to set up Coolify environment with Docker Compose, Nginx Proxy, and two domains. We covered the prerequisites, Docker Compose setup, Nginx configuration, PHP configuration, certificate generation, and testing. With this setup, we were able to handle timeouts for our PHP applications and improve overall performance.