This repo sets up a LEPP (Linux, Nginx, PostgreSQL, PHP) stack using Docker and Docker Compose. This stack is intended for educational purposes and provides a simple environment to develop and test web applications. It serves as a modern alternative to XAMPP, leveraging Docker containers for better isolation and scalability.
- Git
- Docker
- Docker Compose
-
Clone the repository:
git clone https://github.com/run2go/lepp.git cd lepp
-
Start the container stack:
docker-compose up -d
-
Access the services:
- Nginx web server: http://localhost:3000
- Adminer (Database management tool): http://localhost:8080
Docker is a platform that allows you to develop, ship, and run applications inside containers. Here are some basic Docker commands:
- Start a container:
docker start <container_name>
- Stop a container:
docker stop <container_name>
- Run a container:
docker run <image_name>
- Remove a container:
docker rm <container_name>
Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you use a YAML file (docker-compose.yml
) to configure your application's services, networks, and volumes. This simplifies the management of multiple containers and their dependencies.
This stack consists of the following services:
- Nginx: A high-performance web server that serves static files and acts as a reverse proxy for the PHP backend.
- PHP-FPM: A FastCGI Process Manager for PHP that handles PHP requests.
- PostgreSQL: A powerful, open-source object-relational database system.
- Adminer: A full-featured database management tool written in PHP, used as a substitute for phpMyAdmin.
Docker Compose creates a default network for the services defined in the docker-compose.yml
file. The containers can communicate with each other using their service names as hostnames.
Nginx is configured to serve multiple sites:
- Default/Fallback:
Accessible at 127.0.0.1, serves a "Hello World" page. This configuration handles all hostnames that aren't handled otherwise, including127.0.0.1
. Given it takes care of all unhandled addresses, we can also use an address liketest.localhost
- localhost:
Accessible at localhost, provides a PHP info page. The PHP info page is useful for checking the PHP configuration and installed modules. - php.localhost:
Accessible at php.localhost, serves a minimalistic calculator using PHP to compute the inputs when submitting the form. This demonstrates server-side programming with PHP, which can keep sensitive data hidden from the user. - js.localhost:
Accessible at js.localhost, serves a minimalistic calculator using JavaScript to compute the inputs. This demonstrates client-side programming with JavaScript, which is sent as-is to the client.
The PHP-FPM container processes PHP requests forwarded by Nginx. The configuration files for PHP-FPM are located in the php
directory.
We can leverage the fsockopen
function to see if the postgres database is available from our php container. Here is an example script:
<?php
$host = 'postgres';
$port = 5432;
$timeout = 5;
$connection = @fsockopen($host, $port, $errno, $errstr, $timeout);
if ($connection) {
echo "PostgreSQL server is available.";
fclose($connection);
} else {
echo "PostgreSQL server is not available. Error: $errstr ($errno)";
}
?>
Save this script as db_test.php
in your web root directory and access it via http://localhost:3000/db_test.php.
Actual database interactions would require a pgsql client as a php module so we can use functions like pg_connect
directly.
The PostgreSQL container provides a database service. The database is accessible via the Adminer tool.
Adminer is a web-based database management tool that allows you to manage your PostgreSQL database. To access Adminer:
- Open localhost:8080 in your web browser.
- Use the following credentials to log in:
- System: PostgreSQL
- Server: postgres
- Username: postgres
- Password: test_password
- Database: postgres
Adminer provides a user-friendly interface to manage your database, run SQL queries, and perform other database operations.
The stack uses Docker volumes to persist data and configuration files. For simplicity, directories on the host system are mounted directly, and no isolated Docker volumes are used.
- Logging: Logging files and folders are mounted to the
log
folder.log/nginx/
: Nginx logslog/php/
: PHP logslog/postgresql/
: PostgreSQL logs
- Configuration:
docker-compose.yml
: Docker Compose configuration filenginx/
: Nginx configuration and web rootconf.d/
: Nginx site configurationsweb/
: Web root directoryhtml/
: Default sitelocalhost/
: Localhost site*.localhost/
: Subdomain sites
php/
: PHP configuration filesphp.ini
: Custom PHP configurationfastcgi_params
: FastCGI parameters
postgres/
: PostgreSQL data directorydata/
: Persisted PostgreSQL data