Skip to content

Instantly share code, notes, and snippets.

@dangayle
Created December 16, 2012 03:11
Show Gist options
  • Save dangayle/4302849 to your computer and use it in GitHub Desktop.
Save dangayle/4302849 to your computer and use it in GitHub Desktop.
Provision new NGINX server using Fabric
from fabric.api import cd, sudo, run, put
def www(domain):
'''
Create new www file directory
'''
with cd("/var/www/"):
run("mkdir " + domain)
sudo('chown -R www-data:www-data ' + domain)
sudo('chmod -R g+rw ' + domain)
def nginx(domain):
'''
Create new PHP NGINX server in /etc/nginx/ with web directory at /var/www
domain must be www.domain.com
'''
default_config = '''
server {{
server_name {1};
return 301 $scheme://{0}$request_uri;
}}
server {{
server_name {0};
root /var/www/{0};
location / {{
try_files $uri $uri/ /index.php?$args;
}}
access_log /var/log/nginx/{0}.access.log;
error_log /var/log/nginx/{0}.error.log;
include /etc/nginx/www_params;
include /etc/nginx/fastcgi_php;
}}
'''
temp_filename = "/tmp/" + domain
temp_config = open(temp_filename, "w")
# write formatted config file to temp_filename
temp_config.write(default_config.format(domain, domain[4:]))
temp_config.close()
put(temp_filename, "/etc/nginx/sites-available/")
run("ln -s /etc/nginx/sites-available/" + domain + ' /etc/nginx/sites-enabled/' + domain)
www(domain)
sudo('invoke-rc.d nginx reload')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment