Skip to content

Latest commit

 

History

History
397 lines (329 loc) · 11.7 KB

examples.md

File metadata and controls

397 lines (329 loc) · 11.7 KB

Architecture | Features | Examples] | Environment variables | Volumes


Documentation: Examples

  1. Serve static files
  2. Serve PHP files with PHP-FPM
  3. Serve PHP files with PHP-FPM and sync local permissions
  4. Serve PHP files with PHP-FPM over HTTPS
  5. Act as a Reverse Proxy for NodeJS
  6. Act as a Reverse Proxy for Websocket
  7. Fully functional LEMP stack with Mass vhosts
  8. Docker Compose

💡 Serve static files

This example creates the main (default) vhost, which only serves static files.

  • Vhost: main (default)
  • Backend: none

🛈 With no further configuration, the webserver expects files to be served by the main vhost in: /var/www/default/htdocs.

  1. Create a static page
    mkdir -p www/htdocs
    echo '<h1>It works</h1>' > www/htdocs/index.html
  2. Start the webserver
    docker run -d -it \
        -p 9090:80 \
        -v $(pwd)/www:/var/www/default \
        devilbox/apache-2.4
  3. Verify
    curl http://localhost:9090

💡 Serve PHP files with PHP-FPM

This example creates the main (default) vhost, which contacts a remote PHP-FPM host to serve PHP files.

  • Vhost: main (default)
  • Backend: PHP-FPM
PHP-FPM Reference Images

🛈 For this to work, the $(pwd)/www directory must be mounted into the webserver container as well as into the php-fpm container.
🛈 With no further configuration, the webserver expects files to be served by the main vhost in: /var/www/default/htdocs.

  1. Create a helo world page
    mkdir -p www/htdocs
    echo '<?php echo "hello from php";' > www/htdocs/index.php
  2. Start the PHP-FPM container
    docker run -d -it \
        --name php \
        -v $(pwd)/www:/var/www/default \
        devilbox/php-fpm:8.2-base
  3. Start the webserve, linking it to the PHP-FPM container
    docker run -d -it \
        -p 9090:80 \
        -v $(pwd)/www:/var/www/default \
        -e MAIN_VHOST_BACKEND='conf:phpfpm:tcp:php:9000' \
        --link php \
        devilbox/apache-2.4
  4. Verify
    curl http://localhost:9090

💡 Serve PHP files with PHP-FPM and sync local permissions

The same as the previous example, but also ensures that you can edit files locally and have file ownerships synced with webserver and PHP-FPM container.

See Syncronize File System Permissions for details

  • Vhost: main (default)
  • Backend: PHP-FPM
  • Feature: uid and gid are synced

🛈 For this to work, the $(pwd)/www directory must be mounted into the webserver container as well as into the php-fpm container.
🛈 With no further configuration, the webserver expects files to be served by the main vhost in: /var/www/default/htdocs.
🛈 NEW_UID and NEW_GID are set to your local users' value

  1. Create a helo world page
    mkdir -p www/htdocs
    echo '<?php echo "hello from php";' > www/htdocs/index.php
  2. Start the PHP-FPM container
    docker run -d -it \
        --name php \
        -v $(pwd)/www:/var/www/default \
        -e NEW_UID=$(id -u) \
        -e NEW_GID=$(id -g) \
        devilbox/php-fpm:8.2-base
  3. Start the webserve, linking it to the PHP-FPM container
    docker run -d -it \
        -p 9090:80 \
        -v $(pwd)/www:/var/www/default \
        -e NEW_UID=$(id -u) \
        -e NEW_GID=$(id -g) \
        -e MAIN_VHOST_BACKEND='conf:phpfpm:tcp:php:9000' \
        --link php \
        devilbox/apache-2.4
  4. Verify
    curl http://localhost:9090
  5. Explanation: Whenever a file is created by the webserver (e.g.: file uploads) or the PHP-FPM process (e.g.: php creates a file on the filesystem), it is done with the same permissions as your local operating system user. This means you can easily edit files in your IDE/editor and do not come accross permission issues.

💡 Serve PHP files with PHP-FPM over HTTPS

The same as the previous example, just with the addition of enabling SSL (HTTPS).

This example shows the SSL type redir, which makes the webserver redirect any HTTP requests to HTTPS.

Additionally we are mounting the ./ca directory into the container under /ca. After startup you will find generated Certificate Authority files in there, which you could import into your browser.

  • Vhost: main (default)
  • Backend: Reverse Proxy
  • Features: uid and gid are synced and SSL (redirect)

🛈 For this to work, the $(pwd)/www directory must be mounted into the webserver container as well as into the php-fpm container.
🛈 With no further configuration, the webserver expects files to be served by the main vhost in: /var/www/default/htdocs.

  1. Create a helo world page
    mkdir -p www/htdocs
    echo '<?php echo "hello from php";' > www/htdocs/index.php
  2. Start the PHP-FPM container
    docker run -d -it \
        --name php \
        -e NEW_UID=$(id -u) \
        -e NEW_GID=$(id -g) \
        -v $(pwd)/www:/var/www/default \
        devilbox/php-fpm:8.2-base
  3. Start the webserve, linking it to the PHP-FPM container
    docker run -d -it \
        -p 80:80 \
        -p 443:443 \
        -v $(pwd)/www:/var/www/default \
        -v $(pwd)/ca:/ca \
        -e NEW_UID=$(id -u) \
        -e NEW_GID=$(id -g) \
        -e MAIN_VHOST_BACKEND='conf:phpfpm:tcp:php:9000' \
        -e MAIN_VHOST_SSL_TYPE='redir' \
        --link php \
        devilbox/apache-2.4
  4. Verify redirect
    curl -I http://localhost
  5. Verify HTTPS
    curl -k https://localhost

💡 Act as a Reverse Proxy for NodeJS

The following example proxies all HTTP requests to a NodeJS remote backend. You could also enable SSL on the webserver in order to access NodeJS via HTTPS.

  • Vhost: main (default)
  • Backend: Reverse Proxy

🛈 No files need to be mounted into the webserver, as content is coming from the NodeJS server.

  1. Create a NodeJS application
    mkdir -p src
    cat << EOF > src/app.js
    const http = require('http');
    const server = http.createServer((req, res) => {
            res.statusCode = 200;
            res.setHeader('Content-Type', 'text/plain');
            res.write('[OK]\n');
            res.write('NodeJS is running\n');
            res.end();
    });
    server.listen(3000, '0.0.0.0');
    EOF
  2. Start the NodeJS container
    docker run -d -it \
        --name nodejs \
        -v $(pwd)/src:/app \
        node:19-alpine node /app/app.js
  3. Start Reverse Proxy
    docker run -d -it \
        -p 80:80 \
        -e MAIN_VHOST_BACKEND='conf:rproxy:http:nodejs:3000' \
        --link nodejs \
        devilbox/apache-2.4
  4. Verify
    curl http://localhost

💡 Act as a Reverse Proxy for Websocket

The following example proxies all HTTP requests to a Websocket remote backend. You could also enable SSL on the webserver in order to access the websocket backend via HTTPS.

  • Vhost: main (default)
  • Backend: Reverse Proxy (with websocket support)

🛈 No files need to be mounted into the webserver, as content is coming from the websocket server.

  1. Create a websocket server application
    # Create source directory
    mkdir -p src
    
    # websocket server application
    cat << EOF > src/index.js
    const WebSocket = require("ws");
    const wss = new WebSocket.Server({ port: 3000 });
    wss.on("connection", (ws) => {
      ws.send("hello client, you are connected to me");
      ws.on("message", (message) => {
        console.log("New message from client: %s", message);
      });
    });
    console.log("WebSocket server ready at localhost:3000");
    EOF
    
    # package.json
    cat << EOF > src/package.json
    {
      "name": "node-websocket-example",
      "version": "1.0.0",
      "main": "index.js",
      "devDependencies": {},
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "description": "",
      "dependencies": {
        "ws": "^7.5.1"
      }
    }
    EOF
    
    # Startup script
    cat << EOF > src/start.sh
    #!/bin/sh
    npm install
    node index.js
    EOF
  2. Start the Websocket server container
    docker run -d -it \
        --name websocket \
        -v $(pwd)/src:/app \
        -w /app \
        node:19-alpine sh start.sh
  3. Start Reverse Proxy
    docker run -d -it \
        -p 80:80 \
        -e MAIN_VHOST_BACKEND='conf:rproxy:ws:websocket:3000' \
        --link websocket \
        devilbox/apache-2.4
  4. Verify
    # On your host system
    npm install -g wscat
    wscat --connect localhost
    
    Connected (press CTRL+C to quit)
    < hello client, you are connected to me
    >

💡 Fully functional LEMP stack with Mass vhosts

The following example creates a dynamic setup. Each time you create a new project directory below www/, a new virtual host is being created. Additionally all projects will have the .com suffix added to their domain name, which results in <project>.com as the final domain.

  • Vhost: mass (unlimited vhosts)
  • Backend: PHP-FPM

🛈 For this to work, the $(pwd)/www directory must be mounted into the webserver container as well as into the php-fpm container.
🛈 With no further configuration, the webserver expects files to be served by the mass vhost in: /shared/httpd/<project>/htdocs, where <project> is a placeholder for any directory.

  1. Create the project base directory
    mkdir -p www
  2. Start the MySQL container (only for demonstration purposes)
    docker run -d -it \
        --name mysql \
        -e MYSQL_ROOT_PASSWORD=my-secret-pw \
        devilbox/mysql:mariadb-10.10
  3. Start the PHP-FPM container
    docker run -d -it \
        --name php \
        -v $(pwd)/www:/shared/httpd \
        devilbox/php-fpm:8.2-base
  4. Start the webserver container, linking it to the two above
    docker run -d -it \
        -p 8080:80 \
        -v $(pwd)/www:/shared/httpd \
        -e MAIN_VHOST_ENABLE=0 \
        -e MASS_VHOST_ENABLE=1 \
        -e MASS_VHOST_TLD_SUFFIX=.com \
        -e MASS_VHOST_BACKEND='conf:phpfpm:tcp:php:9000' \
        --link php \
        --link mysql \
        devilbox/apache-2.4
  5. Create project-1
    mkdir -p www/project-1/htdocs
    echo '<?php echo "hello from project-1";' > www/project-1/htdocs/index.php
  6. Verify project-1
    curl -H 'Host: project-1.com' http://localhost:8080
  7. Create another
    mkdir -p www/another/htdocs
    echo '<?php echo "hello from another";' > www/another/htdocs/index.php
  8. Verify another
    curl -H 'Host: another.com' http://localhost:8080
  9. Add more projects as you wish...

💡 Docker Compose

Have a look at the examples directory. It is packed with all kinds of Docker Compose examples:

  • SSL
  • PHP-FPM remote server
  • Python and NodeJS Reverse Proxy
  • Mass virtual hosts
  • Mass virtual hosts with PHP-FPM, Python and NodeJS as backends