Semi-automating Nginx server block set up on Ubuntu with a script?

August 31, 2014 3.9k views

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:

  1. sudo mkdir -p /var/www/domainnamehere.com/html
  2. sudo chown -R $USER:$USER /var/www/domainnamehere.com/html
  3. sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/domainnamehere.com
  4. sudo nano /etc/nginx/sites-available/domainnamehere.com to add references to the directory made it step #1 and add the domain name to look for next server_name.
  5. sudo ln -s /etc/nginx/sites-available/domainnamehere.com /etc/nginx/sites-enabled/

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!

1 Answer

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:



# 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;


# 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.

  • This is EXACTLY what I was looking for. Thanks so much!

    One follow up question, where is a "best practice" place to store this script? In the ~ directory? Or does it not really matter that much?

    Also, not sure if any mods are reading this...but if somebody could correct my typo in the title ("bock" instead of "block") it might help other people find this great answer.

  • Glad you found it helpful! I've edited your post title as requested.

    As for the script location, I think you can put it where ever makes sense for you. If that's your home directory, that's fine. A common practice is to create create a directory called bin in your home directory and put user-specific scripts there. You can then add it to your path if it is not already there:

    mkdir ~/bin
    mv script.sh ~/bin
    echo PATH=~/bin:$PATH >> ~/.bashrc
    source ~/.bashrc

    This will add the ~/bin directory to your path each time your bash session is initiated.

    If you would like it in a globally-accessible location though, a good choice would either be /usr/local/bin or perhaps /usr/local/sbin`. These are usually already in the path of your user.

  • Script worked great! I had to make some minor adjustments to get some of the server block file to work right.

    Seems like anything that needed a literal dollar sign symbol printed gave it problems.

    For example, with this:

                    try_files $uri $uri/ =404;

    The "$uri" stuff wasn't output to the file, so I did this instead:

                    try_files \$uri \$uri/ =404;

    Which did the trick. Thanks again!

Have another answer? Share your knowledge.