This project provides automation scripts and configurations for setting up a multi-node server environment with Docker, Nginx, PHP, PostgreSQL, and Redis.
- Multipass for launching virtual machines locally
- Ubuntu 22.04 LTS
- Sufficient system resources (at least 8GB RAM and 100GB storage recommended)
Launch two Ubuntu 22.04 instances using Multipass:
multipass launch 22.04 -n primary -c 2 -m 4G -d 50G --name master-instance
multipass launch 22.04 -n primary -c 2 -m 4G -d 50G --name node-instance
Open two terminal windows and connect to each instance:
multipass shell master-instance # Master instance
multipass shell node-instance # Node instance
Clone this repository and run the packages-installation.sh
script on both instances:
git clone https://github.com/David-Melamed/docker-nginx-php-postgres-setup.git
cd docker-nginx-php-postgres-setup
chmod +x packages-installation.sh
Before running the installation script, make sure to review and update the versions.conf file in the package-installation directory to specify the desired versions for each package. If no version is specified, the latest version will be used.
To start the installation process, run the main.sh script:
chmod +x main.sh
sudo ./main.sh
During the installation process, you will be prompted to:
- Specify whether the current instance is a master or node server.
- Choose between installing all packages or performing a custom installation.
- If you select "all", all packages will be installed with their default versions specified in versions.conf.
- If you select "custom", you will be prompted to choose which packages to install and optionally provide custom versions for each package.
Note: The package-installation directory contains the following subdirectories:
downloads/: Stores downloaded package files during the installation process. logs/: Stores log files generated during the server setup and package installation. temp/: Stores temporary files used during the package installation process. scripts/: Contains individual package installation scripts.
Run the following command to create the necessary directory and add the info.php file with the phpinfo() function:
sudo mkdir -p /var/www/html && sudo sh -c "echo '<?php phpinfo(); ?>' > /var/www/html/info.php"
- Copy the files to the master instance:
scp -r master/Docker/* user@master-instance-ip:~/
- Build the Docker image:
sudo docker build -t my-nginx-site .
- Run the container:
sudo docker run -d -p 8081:80 -v $(pwd)/index.html:/usr/share/nginx/html/index.html my-nginx-site
- Copy the files to the node instance:
scp -r node/Docker/* user@node-instance-ip:~/
- Build the Docker image:
sudo docker build -t my_nginx_postgres .
- Run the container:
sudo docker run -d -p 8081:80 -p 5432:5432 -v /var/run/postgresql:/tmp --user root --name postgres my_nginx_postgres
- Copy the Nginx configuration:
sudo cp master/nginx/sites-enabled/default /etc/nginx/sites-enabled/default OR sudo cp node/nginx/sites-enabled/default /etc/nginx/sites-enabled/default
- Validate Nginx configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
-
Docker connectivity between master and node instances
- Access
http://<MASTER-IP>/info-node1.php
to display PHP info from the node instance's Docker container
- Access
-
PostgreSQL on the node instance
- Send requests from the master instance to the PostgreSQL container on the node instance:
http://<MASTER-IP>/server2-postgres-tables
- This should list all databases in the node instance's PostgreSQL container
- Send requests from the master instance to the PostgreSQL container on the node instance: