First of all, thanks for this great tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts
I’m following it to manually create server blocks for multiple domains on the same server.
I can’t help but think I could be saving time by writing a script one time and running a command like “myscript domainnamehere.com” which would end up doing all of the steps for me. Here’s what I currently do:
Obviously #1 and #2 is easy, but #3 and #4 is where I think things can be made more efficient.
Also I know I probably don’t need to use the cp command to copy the site configuration over, it would probably make more sense if the sample file was included in the script so it could easily write to a file with the proper domain name variables.
How would I develop a script that could take an argument (the domain name) and do all of the above for me?
Does this make sense at all? Let me know if you need any clarification about anything. Any pointers would be greatly appreciated!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I have a similar situation that I need to automate the Nginx installation with default port of 8080 instead of 80. The following pre-check needs to be fulfilled.
Any helps are greatly appreciated. Thank you.
Hey lelandf:
I gave a go at a quick script that works as you describe. I decided to go the route of writing the Nginx server block file directly from the script using a bash here document.
It is by no means a pretty script, but it generally does what you’re manually doing:
#!/bin/bash
domain=$1
root="/var/www/$domain/html"
block="/etc/nginx/sites-available/$domain"
# Create the Document Root directory
sudo mkdir -p $root
# Assign ownership to your regular user account
sudo chown -R $USER:$USER $root
# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF
server {
listen 80;
listen [::]:80;
root /var/www/$domain/html;
index index.html index.htm;
server_name $domain www.$domain;
location / {
try_files $uri $uri/ =404;
}
}
EOF
# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/
# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload
You can put that in a file and then make it executable with chmod:
chmod +x script.sh
You should then be able to generate new server blocks by typing:
./script.sh <your_domain_here>
There are definitely improvements that can be made, but let me know how it works out for you.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.