Skip to content

Instantly share code, notes, and snippets.

@WillSams
Last active January 25, 2019 23:51
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 WillSams/052d71a7012e2530c49bafc90e52d93f to your computer and use it in GitHub Desktop.
Save WillSams/052d71a7012e2530c49bafc90e52d93f to your computer and use it in GitHub Desktop.
Install script for Nginx & GrandNode .NET for Ubuntu 18.04 'bionic'
#!/bin/bash
# Nginx installer script - Grandnode.NET example
#
# Pre-req:
# * Ubuntu Bionic (18.04) flavored distro
# * Script must be executed under the account of the web admininstrator
# * linux-devel-setup.sh - https://gist.github.com/WillSams/f17427b5a4f5bccc23d6efe1389b33ca
# * mongodb-setup.sh - https://gist.github.com/WillSams/3588b69c805acf07ccb278c55fd91302
#
# Note: If you don't have Nginx installed, this script will install and do basic setup for you.
#
# Additional notes:
# If you want to setup Nginx w/ KeystoneJS (NodeJS), see my nginx-keystone.sh gist.
# If you want to setup Nginx w/ Rails (Ruby), see my nginx-rails.sh gist.
# Running the two above scripts on the same web server won't break this script.
set -o nounset # unset variables are errors
__ScriptVersion="2018.10.19"
__ScriptName="nginx-grandnode.sh"
__ScriptFullName="$0"
if [ -d /etc/nginx ]; then
echo "Nginx is already installed on this system."
else
echo "**************************** NGINX ******************************"
echo "Installing nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
echo "**************************** NGINX ******************************"
echo "Installing nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
sudo bash -c "add-apt-repository ppa:certbot/certbot -y"
sudo sudo bash -c "apt update && apt upgrade -y"
sudo bash -c "apt install nginx python-certbot-nginx -y"
sudo bash -c "ufw allow 'NGINX FULL' && ufw enable"
sudo bash -c "rm -v /etc/nginx/sites-available/default"
sudo bash -c "sed -e '/include \/etc\/nginx\/sites-enabled/ s/^#*/#/' -i /etc/nginx/nginx.conf"
sudo bash -c "echo '#Image Caching
include \/etc\/nginx\/conf.d\/img-cache.conf;' >> /etc/nginx/nginx.conf"
sudo bash -c "echo 'location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 90d;
add_header Pragma public;
add_header Cache-Control \"public\";
}' >> /etc/nginx/conf.d/img-cache.conf"
touch /etc/nginx/conf.d/default.conf
sudo bash -c "systemctl enable nginx && service nginx start"
fi
if [ -f /etc/systemd/system/example-dotnet.service ]; then
echo "Web project 'example-dotnet' appears to be already served."
else
echo "*********************** EXAMPLE-DOTNET **************************"
echo "Installing the example-dotnet.com project to the nginx web server."
echo "You may be prompted for root credentials to complete the install."
echo "*****************************************************************"
git clone https://github.com/grandnode/grandnode example-dotnet.com
sudo bash -c "mkdir /var/www/example-dotnet.com"
sudo bash -c "chown www-data:www-data -R /var/www/example-dotnet.com"
cd example-dotnet.com
dotnet publish Grand.Web -o /var/www/example-dotnet.com -c Release
sudo bash -c "echo '##################################
upstream example-dotnet {
server 127.0.0.1:5000 fail_timeout=0;
}
server {
listen 80;
server_name example-dotnet.com;
root /var/www/example-dotnet.com;
try_files "'$uri'"/index.html "'$uri'" @example-dotnet;
location @example-dotnet {
proxy_set_header X-Forwarded-For "'$proxy_add_x_forwarded_for'";
proxy_set_header Host "'$http_host'";
proxy_redirect off;
proxy_pass http://127.0.0.1:5000;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
##################################' >> /etc/nginx/conf.d/default.conf"
sudo bash -c "echo '[Unit]
Description=GrandNode
[Service]
WorkingDirectory=/var/www/example-dotnet.com/
ExecStart=/usr/bin/dotnet /var/www/example-dotnet.com/Grand.Web.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-example-dotnet.com
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target' >> /etc/systemd/system/example-dotnet.service"
sudo bash -c "systemctl enable example-dotnet.service && service example-dotnet start"
sudo bash -c "service nginx restart"
sudo bash -c "echo '
0.0.0.0 example-dotnet.com' >> /etc/hosts"
fi
sudo sudo bash -c "apt autoremove && apt clean -y"
echo "Script complete. Visit the website locally on the server at http://example-dotnet.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment