PHPackages                             jturbide/vhost - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jturbide/vhost

ActiveLibrary

jturbide/vhost
==============

A command-line tool built with Symfony Console that automatically generates Apache and Nginx virtual host configurations from YAML definitions, updates system hosts files, restarts web servers, and manages PHP caches (OPCache, APCu, sessions). Supports both Linux and Windows environments.

1.0.0(1y ago)120BSD-3-ClausePHP

Since Mar 21Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jturbide/vhost)[ Packagist](https://packagist.org/packages/jturbide/vhost)[ RSS](/packages/jturbide-vhost/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Vhost Generator CLI
===================

[](#vhost-generator-cli)

A command-line tool built with Symfony Console that automatically generates Apache and Nginx virtual host configurations from YAML definitions, updates system hosts files, restarts web servers, and manages PHP caches (OPCache, APCu, sessions). Supports both Linux and Windows environments.

---

Features
--------

[](#features)

- Cross-platform: runs on Linux and Windows.
- Generates vhost configs for both Apache and Nginx from simple YAML definitions.
- Injects global config snippets into each server’s main config file (placing them next to confFile).
- Updates /etc/hosts or C:\\Windows\\System32\\drivers\\etc\\hosts for local domain resolution.
- Respects existing files; uses backups and --force to prevent accidental overwrites.
- Clears PHP caches (OPCache, APCu, sessions) on multiple PHP versions (Linux with Remi).
- Service restarts: can optionally restart Apache, Nginx, and PHP-FPM.
- Verbosity levels with colored \[INFO\], \[OK\], \[WARNING\], and \[ERROR\] logs.

---

Requirements
------------

[](#requirements)

- PHP 8.0+
- Composer (to install dependencies)
- Symfony Console and Symfony YAML libraries (handled by composer.json)
- Linux: systemctl, apachectl, nginx commands for restarts/tests (or suitable equivalents)
- Windows: net stop, net start with correct service names
- Two YAML config files: config.yaml and sites.yaml

---

Installation
------------

[](#installation)

1. Clone or download this repository.
2. Run 'composer install'.
3. Ensure 'bin/vhost.php' is executable (chmod +x if on Linux).
4. Make sure 'config.yaml' and 'sites.yaml' exist in the root directory.

---

Configuration Files
-------------------

[](#configuration-files)

### config.yaml

[](#configyaml)

Defines per-platform (Linux, Windows) settings, including main config files, vhost directories, service names, default SSL certs, optional global config snippets, and your templates.

Example minimal structure:

```
platforms:
    linux:
        rootDir: /mnt/web/
        hostsFile: /etc/hosts

        apacheEnabled: true
        nginxEnabled: true

        apache:
          confFile: /etc/httpd/conf/httpd.conf
          confDir: /etc/httpd/conf.d/vhosts
          globalSnippet: |
            # Additional directives for Apache
          logDir: /var/log/httpd
          templateFile: templates/linux/apache.conf

        nginx:
          confFile: /etc/nginx/nginx.conf
          confDir: /etc/nginx/conf.d
          globalSnippet: |
            # Additional directives for Nginx
          logDir: /var/log/nginx
          templateFile: templates/linux/nginx.conf

        services:
          apache: httpd
          nginx: nginx

        defaultSsl:
          certificate: /etc/ssl/default.crt
          certificateKey: /etc/ssl/default.key

    windows:
        rootDir: "C:\\Dev\\Web\\"
        hostsFile: "C:\\Windows\\System32\\drivers\\etc\\hosts"

        apacheEnabled: true
        nginxEnabled: true

        apache:
          confFile: "C:\\httpd\\conf\\httpd.conf"
          confDir: "C:\\httpd\\conf\\vhosts"
          globalSnippet: |
            # Windows Apache directives
          logDir: "C:\\httpd\\logs"
          templateFile: "templates\\windows\\apache.conf"

        nginx:
          confFile: "C:\\nginx\\conf\\nginx.conf"
          confDir: "C:\\nginx\\conf.d"
          globalSnippet: |
            # Windows Nginx directives
          logDir: "C:\\nginx\\logs"
          templateFile: "templates\\windows\\nginx.conf"

        services:
          apache: "Apache2.4"
          nginx: "Nginx"

        defaultSsl:
          certificate: "C:\\ssl\\default.crt"
          certificateKey: "C:\\ssl\\default.key"

servers:
localhost: 127.0.0.1
```

---

### sites.yaml

[](#sitesyaml)

Lists all the domains to configure, their document root paths, any aliases, custom SSL certs, and optional 'custom' snippet.

Example structure:

```
sites:
- name: myapp.local
  root: MyApp/public/
  php: "83"
  variables:
  custom: |
      # Site-level directives
      php_value upload_max_filesize 128M

- name: admin.myapp.local
  root: MyApp/admin/public/
  aliases:
    - backoffice.myapp.local
      php: "83"
      variables:
      ssl_certificate: /etc/ssl/myapp.crt
      ssl_certificate_key: /etc/ssl/myapp.key
      custom: |
      # Admin-specific directives
```

---

Usage
-----

[](#usage)

Run the command from your project root:

```
php bin/vhost.php [options]
```

### CLI Options

[](#cli-options)

`--force, -f`
Overwrite any existing files (site vhosts, global snippets, main config blocks).

`--no-backup`
Disable creating .bak-YYYYmmddHHMMSS copies before overwriting.

`--os=VALUE`
Override OS detection with 'linux' or 'windows'. Defaults to PHP\_OS\_FAMILY.

`--restart, -r`
Restart Apache/Nginx after config generation.

`--restart-php`
Restart discovered PHP-FPM services on Linux.

`--clear-opcache`
Clear OPCache on all discovered PHP versions (Remi-based).

`--clear-apcu`
Clear APCu on all discovered PHP versions (Remi-based).

`--clear-sessions`
Remove session files based on each version’s session.save\_path.

### Examples

[](#examples)

1. Basic generation (no backups, no restarts):

    ```
    php bin/vhost.php --no-backup
    ```
2. Force overwrite and restart services:

    ```
    php bin/vhost.php --force --restart
    ```
3. Clear all caches (OPCache, APCu, sessions):

    ```
    php bin/vhost.php --clear-opcache --clear-apcu --clear-sessions
    ```
4. Manually set OS:

    ```
    php bin/vhost.php --os=windows
    ```

---

How It Works
------------

[](#how-it-works)

### Global Snippet Injection

[](#global-snippet-injection)

Reads your `config.yaml` for each server (apache or nginx). Places your `globalSnippet` text into a file named `global-apache.conf` or `global-nginx.conf` located in the same directory as `confFile`. Then injects an `Include` or `include` line into your main config, marked by `# BEGIN AUTOGENERATED-apache` or `# BEGIN AUTOGENERATED-nginx`.

### Site Vhost Generation

[](#site-vhost-generation)

For each site in sites.yaml:

- Loads the templateFile (e.g. `templates/linux/apache.conf`).
- Fills placeholders like `{{name}}`, `{{root}}`, `{{ssl_certificate}}`, etc.
- Merges optional `{{custom}}` snippet if defined.
- Writes the final `.conf` to the confDir (e.g. `/etc/httpd/conf.d/vhosts/myapp.local.conf`).
- Skips overwriting unless `--force`. If doc root doesn't exist, logs a warning and skips.

### Hosts File Updates

[](#hosts-file-updates)

Appends domain entries to your system hosts file in a block marked `# BEGIN WEB.LOCAL` / `# END WEB.LOCAL`. Respects backups if needed. Uses the IP from `servers.localhost` in `config.yaml` by default, or a custom `ip` in `sites.yaml` if present.

### Service Restarts

[](#service-restarts)

If `--restart` is specified:

- On Linux, runs 'apachectl configtest &amp;&amp; systemctl restart httpd' and 'nginx -t &amp;&amp; systemctl restart nginx' (or whatever service names are set in config.yaml).
- On Windows, runs 'net stop' and 'net start' for each service name.

If `--restart-php` is used, it restarts any discovered 'php.\*fpm' services on Linux.

### PHP Cache Clearing

[](#php-cache-clearing)

If `--clear-opcache` or `--clear-apcu`, the script runs `php -r ...` in each discovered `/opt/remi/phpXX` version to reset OPCache or APCu. If `--clear-sessions`, removes session files in each version’s session.save\_path (parsed from php.ini). This feature is Linux-specific by default.

---

Advanced Topics
---------------

[](#advanced-topics)

### Overwriting and Backups

[](#overwriting-and-backups)

- By default, files are not overwritten if they exist. Use `--force` to overwrite.
- If backups are enabled, the script copies existing files to filename.bak-YYYYmmddHHMMSS.

### Directory Checks

[](#directory-checks)

- If the site document root doesn’t exist, the script logs a warning and skips that site’s vhost generation.

### Customization

[](#customization)

- Add more placeholders to your template files (e.g. `{{myVar}}`).
- Modify the script to parse them from site or config variables as needed.
- Adjust session clearing logic to match your system’s layout if not using Remi or if on Windows.

---

Installing Apache Lounge on Windows and Setting Up the Service
--------------------------------------------------------------

[](#installing-apache-lounge-on-windows-and-setting-up-the-service)

### Step-by-Step Guide

[](#step-by-step-guide)

1. **Download Apache from Apache Lounge**

    - Visit [Apache Lounge](https://www.apachelounge.com/) and download the appropriate version of Apache for your system (e.g., 64-bit or 32-bit).
2. **Extract Apache Files**

    - Extract the downloaded `.zip` file to a desired location (e.g., `C:\httpd\`).
3. **Configure Apache**

    - Open the `httpd.conf` file located in the `conf` directory of the extracted files (e.g., `C:\httpd\conf\httpd.conf`).
    - Modify the following lines to suit your environment:
        - Update the `ServerRoot` to the extracted path, e.g.:

            ```
            ServerRoot "C:/httpd"

            ```
        - Update the `DocumentRoot` to your desired root directory, e.g.:

            ```
            DocumentRoot "C:/Dev/Web"

            ```
        - Ensure you have configured your virtual host settings correctly (refer to the relevant sections in this document).
4. **Install the Service**

    - Open a Command Prompt with Administrator privileges.
    - Navigate to the `bin` directory of the extracted Apache folder, e.g.: ```
        cd C:\httpd\bin
        ```
    - Run the following command to install Apache as a Windows service: ```
        httpd.exe -k install
        ```
5. **Verify Installation**

    - After running the command, Apache is registered as a Windows service.
    - Open the Windows Services Manager (press `Win + R`, type `services.msc`, and hit Enter).
    - Look for the service named `Apache2.4` (or the name defined in your Apache setup).
    - Confirm that the service appears as "Automatic" in the Startup Type column.
6. **Start the Service**

    - You can start Apache in any of the following ways:
        - Run the command in the `bin` directory: ```
            httpd.exe -k start
            ```
        - Or, start the service from the Windows Services Manager by right-clicking on `Apache2.4` and selecting " Start."
7. **Test Your Setup**

    - Open a web browser and navigate to `http://localhost`.
    - If everything is configured correctly, you should see the Apache default page or your defined `DocumentRoot`content.

### Uninstalling the Apache Service

[](#uninstalling-the-apache-service)

- If you need to remove the Apache service, navigate to the `bin` directory and run: ```
    httpd.exe -k uninstall
    ```

This should properly unregister Apache as a Windows service.

---

License
-------

[](#license)

This sample project is provided as is, under no specific license unless you add one. Feel free to incorporate it into your private or commercial workflow.

For any questions or suggestions, please open an issue or contact the maintainer.

---

Enjoy automating your virtual host configurations across Linux and Windows!

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance45

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

423d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccc75eba63e8de9a814c4809b47be69bd384a60c8f6dcaf1b5abeb257e204772?d=identicon)[jturbide](/maintainers/jturbide)

---

Top Contributors

[![jturbide](https://avatars.githubusercontent.com/u/4306188?v=4)](https://github.com/jturbide "jturbide (13 commits)")

### Embed Badge

![Health badge](/badges/jturbide-vhost/health.svg)

```
[![Health](https://phpackages.com/badges/jturbide-vhost/health.svg)](https://phpackages.com/packages/jturbide-vhost)
```

###  Alternatives

[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[laravel/sail

Docker files for running a basic Laravel application.

1.9k186.9M1.0k](/packages/laravel-sail)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
