Skip to content

Instantly share code, notes, and snippets.

@mikesorae
Created June 27, 2014 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesorae/218ba3b18933d879f29b to your computer and use it in GitHub Desktop.
Save mikesorae/218ba3b18933d879f29b to your computer and use it in GitHub Desktop.
prepare new user and config file for nginx
#!/bash/bin
if [ $# != 2 ]; then
echo "usage: $0 app_name app_port" 1>&2
exit 0
fi
app_name=$1
port=$2
echo "checking the port"
port_used=`lsof -iTCP:${port} | wc -c`
if [ $port_used -ne 0 ]; then
echo "the port is already used"
exit 1
fi
echo "checking the user"
user_exists=false
getent passwd $app_name >/dev/null 2>&1 && user_exists=true
if $user_exists; then
echo "this user already exists"
exit 1
fi
echo "preparing user for the site"
useradd $app_name
gpasswd -a nginx $app_name
echo "preparing setting for nginx"
nginx_conf="/opt/nginx/conf"
cp $nginx_conf/rails_template.conf $nginx_conf/$app_name.conf
sed -i -E "s/\\\$PORT/${port}/" $nginx_conf/$app_name.conf
sed -i -E "s/\\\$APP_HOME/${app_name}\/${app_name}\/public/" $nginx_conf/$app_name.conf
sed -i -E "s/(#app_conf_setting)/\1\n include ${app_name}.conf;/" /opt/nginx/conf/nginx.conf
service nginx restart
@mikesorae
Copy link
Author

echo 1>&2
exit 1
?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment