Skip to content

Instantly share code, notes, and snippets.

@prionkor
Last active September 23, 2022 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prionkor/851dd952e5bd313b26eb623baf869e6d to your computer and use it in GitHub Desktop.
Save prionkor/851dd952e5bd313b26eb623baf869e6d to your computer and use it in GitHub Desktop.
Ubuntu - Nginx - PHP 7.4 - New Domain Script
#!/bin/bash
# Info
# ---
# script can run with the domain as a command line input
# `sudo ./nginx_domain.sh my_domain.com` or without and
# the script will prompt the user for input
#config
web_root='/var/www'
config_dir='/etc/nginx'
if [ -z "$1" ]
then
#user input
echo -e "Enter domain name:"
read DOMAIN
echo "Creating Nginx domain settings for: $DOMAIN"
if [ -z "$DOMAIN" ]
then
echo "Domain required"
exit 1
fi
fi
if [ -z "$DOMAIN" ]
then
DOMAIN=$1
fi
(
cat <<EOF
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
server_name $DOMAIN www.$DOMAIN;
root $web_root/$DOMAIN/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log $web_root/$DOMAIN/log/error_log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
}
EOF
) > $config_dir/sites-available/$DOMAIN.conf
echo "Making web directories"
mkdir -p $web_root/"$DOMAIN"
mkdir -p $web_root/"$DOMAIN"/{public,private,log,backup}
ln -s $config_dir/sites-available/"$DOMAIN".conf $config_dir/sites-enabled/"$DOMAIN".conf
/etc/init.d/nginx reload
echo "Nginx - reload"
chown -R www-data:www-data $web_root/"$DOMAIN"
chmod 755 $web_root/"$DOMAIN"/public
echo "Permissions have been set"
echo "$DOMAIN has been setup"
@prionkor
Copy link
Author

How To Install

  1. Upload the script to /root/bin directory and rename it to nvhost
  2. Set the script as executable. chmod +x nvhost
  3. Add your file to PATH
  4. Now you can just type nvhost in your terminal ( with root excess (sudo) ).

The script will ask for domain name to input. Once you give it it will do the rest and create the vhost for you.

Enjoy! :)

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