In my testing and building efforts I frequently find myself creating a virtual host like
http://myproject.local. It takes a few minutes but apart from a couple of strings it’s always the same process. Time for a bash script that does this for us!
I wrote a script that automates the task of creating a directory for the website, creating the config file, enabling the site, restarting the server and installing WordPress. There are a couple of prerequisites and a few things to be aware of so before you run it, do read my notes after the code.
You invoke the bash script like this:
sudo bash make_site.sh mysite local yes
The three parameters are:
- The name of the site
- The TLD of the site
- Wether or not you want the WordPress install
The result of this command will be a website at mysite.local with WordPress installed and running. There are a few things to note before running this script:
- It will create the folder for the website at /var/www/html/website_name. If you have a different setup, change the folder in line 2, the DocumentRoot on line 12 and the Directory directive on line 17.
- You must have WP-CLI installed for the WordPress install to work.
- If you do not want the WordPress install just omit the third parameter. Setting it to no or false will notwork.
The script does the following things:
- Creates a directory for the site
- Adds a config file for the site
- Enables the site and restarts the server
- Optionally installs WordPress
- The user and password for the database is set to root because that’s what it is on my Vagrant box. Change this on line 37 if needed
- The database name is the same as the site name. Change this on line 37 if needed
- Change the details of your admin user on line 39
- The Hello Dolly And Akismet plugins are removed
If you have any idea to make this better I’m all ears!