π Automate Apache/Nginx Virtual Hosts in Seconds (No More Manual Setup!)
If you're a System engineer/Administrator working with local/production servers, you already know the pain:
- Create project folder
- Write Apache/Nginx config
- Edit
/etc/hosts - Enable site
- Restart server
Repeat. Again. And again.
Itβs boring, error-prone, and honestlyβ¦ a waste of time.
Thatβs exactly why I built VirtualHost Automation β a simple script to automate the entire workflow.
β‘ Why Automate Virtual Hosts?
Setting up virtual hosts manually involves multiple steps like:
- Creating directories under
/var/www - Writing
.conffiles inside/etc/apache2/sites-available - Editing system host files
- Reloading Apache
These steps are repetitive and can easily break if you miss something. Many developers solve this with shell scripts that handle everything in one go. ([DEV Community][1])
β‘ The goal: one command = fully working virtual host
I created this:
π https://github.com/shuvo-halder/virtualhost-automation
π§ What This Project Does
The virtualhost-automation script automates:
β
Virtual host creation
β
Apache configuration setup
β
Host file entry
β
Directory initialization
β
Server reload
Instead of doing 6β7 manual steps, you just run one command.
π οΈ How It Works (Apache server behind the Scenes)
At a high level, the script follows a typical automation flow:
1. Take Input
You provide:
- Site name (e.g.
project.dev) - IP or host mapping
2. Create Project Structure
- Generates
/var/www/project.dev - Adds a default index file (optional)
3. Generate Apache Config
- Copies template config
- Replaces variables dynamically
This is usually done using tools like sed for string replacement. ([DEV Community][1])
4. Update /etc/hosts
- Maps domain β local IP
5. Enable Site + Reload Apache
- Runs
a2ensite - Reloads Apache instantly
π₯ Example Workflow
Instead of doing all this manually:
mkdir /var/www/project.dev
nano /etc/apache2/sites-available/project.dev.conf
nano /etc/hosts
a2ensite project.dev.conf
systemctl reload apache2
You just run:
./virtualhost.sh project.dev
Boom. Done. π―
π‘ Why This Matters
Automation isnβt just about saving time β it improves consistency and reduces mistakes.
Many modern dev environments rely on automation tools because:
- Manual setup doesnβt scale
- DevOps workflows demand speed
- Reproducibility is critical
Even popular tools and scripts in the ecosystem aim to solve the same problem β simplifying virtual host creation with a single command. ([Libraries.io][2])
π§ͺ Use Cases
This project is especially useful if you:
- Build multiple local projects
- Work with Laravel / PHP / Node apps
- Frequently test domains locally
- Manage multiple dev/prod environments
π§Ύ Final Thoughts
Virtual host setup shouldnβt slow you down.
With tools like virtualhost-automation, you:
- Save time
- Reduce errors
- Focus on actual development
Automation is not optional anymore β itβs a necessity.
β Check Out the Repo
π https://github.com/shuvo-halder/virtualhost-automation
If this helps your workflow, give it a star β
and feel free to contribute or customize it for your setup.
Top comments (0)