DEV Community

Shuvo
Shuvo

Posted on

Automate Apache/Nginx Virtual Server host in Seconds.

πŸš€ 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 .conf files 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
Enter fullscreen mode Exit fullscreen mode

You just run:

./virtualhost.sh project.dev
Enter fullscreen mode Exit fullscreen mode

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)