|
| 1 | + |
| 2 | +#!/bin/sh |
| 3 | + |
| 4 | +# Navigate to the project directory |
| 5 | +project_dir=$(pwd) |
| 6 | +venv_dir=$project_dir/.venv |
| 7 | + |
| 8 | +# Warning to ensure you are at the root of the project |
| 9 | +read -p "Please ensure $project_dir IS THE ROOT OF THE REPO before proceeding. Continue? (y/n): " answer |
| 10 | +case ${answer:0:1} in |
| 11 | + y|Y ) |
| 12 | + echo "Proceeding with the script..." |
| 13 | + ;; |
| 14 | + * ) |
| 15 | + echo "Exiting the script..." |
| 16 | + exit |
| 17 | + ;; |
| 18 | +esac |
| 19 | + |
| 20 | + |
| 21 | +# Check if the virtual environment exists in the project directory |
| 22 | +if [ ! -d "$venv_dir" ]; then |
| 23 | + echo "Virtual environment not found in project directory!" |
| 24 | + exit |
| 25 | +fi |
| 26 | + |
| 27 | +deploy_dir=$project_dir/.deploy/.gi-generated |
| 28 | +mkdir -p $deploy_dir &2> /dev/null |
| 29 | + |
| 30 | + |
| 31 | +# Update the package lists for upgrades and new package installations |
| 32 | +sudo apt update -y |
| 33 | +# Ensure libssl-dev is installed |
| 34 | +sudo apt install libssl-dev |
| 35 | + |
| 36 | +# Check if nginx is installed, if not then install it |
| 37 | +if ! command -v nginx &> /dev/null |
| 38 | +then |
| 39 | + sudo apt install -y nginx |
| 40 | +fi |
| 41 | + |
| 42 | +. $venv_dir/bin/activate |
| 43 | +# Install uwsgi and gevent using pip |
| 44 | +pip install uwsgi gevent |
| 45 | +# not recommended: asyncio greenlet |
| 46 | +deactivate |
| 47 | + |
| 48 | +# Remove existing uwsgi link if it exists and create a new one |
| 49 | +sudo rm -f /usr/bin/uwsgi |
| 50 | +sudo ln -s $venv_dir/bin/uwsgi /usr/bin/uwsgi |
| 51 | + |
| 52 | +# Generate a Systemd file for uWSGI |
| 53 | +echo """ |
| 54 | +[Unit] |
| 55 | +Description=Taxplorer UWSGI Server |
| 56 | +After=syslog.target |
| 57 | +
|
| 58 | +[Service] |
| 59 | +ExecStart=uwsgi --master --http :5000 --gevent 1000 --http-websockets --module main:web_app --logto /tmp/taxplorer.log |
| 60 | +WorkingDirectory=$(pwd)/app |
| 61 | +Restart=always |
| 62 | +KillSignal=SIGQUIT |
| 63 | +Type=notify |
| 64 | +StandardError=syslog |
| 65 | +NotifyAccess=all |
| 66 | +User=$(whoami) |
| 67 | +
|
| 68 | +[Install] |
| 69 | +WantedBy=multi-user.target |
| 70 | +""" > $deploy_dir/uwsgi/taxplorer.uwsgi.service |
| 71 | + |
| 72 | +# Move the Systemd file to the correct directory |
| 73 | +sudo cp $deploy_dir/uwsgi/taxplorer.uwsgi.service /etc/systemd/system/taxplorer.uwsgi.service |
| 74 | +sudo systemctl daemon-reload |
| 75 | + |
| 76 | +# Start the uWSGI service |
| 77 | +sudo systemctl restart taxplorer.uwsgi.service |
| 78 | + |
| 79 | +# Enable the uWSGI service to start on boot |
| 80 | +sudo systemctl enable taxplorer.uwsgi.service |
| 81 | + |
| 82 | +# Create a self-signed SSL certificate |
| 83 | +#sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/certs/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt |
| 84 | +sudo openssl req -x509 -newkey rsa:4096 -keyout /etc/ssl/certs/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt -sha256 -days 3650 -nodes -subj "/CN=localhost" |
| 85 | +# Update the Nginx configuration to expose the application |
| 86 | +echo """ |
| 87 | +server { |
| 88 | + listen 80; |
| 89 | + listen 443 ssl; |
| 90 | +
|
| 91 | + server_name localhost; |
| 92 | +
|
| 93 | + # SECURITY HEADERS |
| 94 | + add_header 'X-Frame-Options' 'SAMEORIGIN'; |
| 95 | + add_header 'X-XSS-Protection' '1; mode=block'; |
| 96 | + add_header 'X-Content-Type-Options' 'nosniff'; |
| 97 | + add_header 'Referrer-Policy' 'same-origin'; |
| 98 | + add_header 'Strict-Transport-Security' 'max-age=63072000'; |
| 99 | +
|
| 100 | + ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt; |
| 101 | + ssl_certificate_key /etc/ssl/certs/nginx-selfsigned.key; |
| 102 | +
|
| 103 | + location / { |
| 104 | + proxy_pass http://localhost:5000; |
| 105 | + proxy_set_header Upgrade \$http_upgrade; |
| 106 | + proxy_set_header Connection 'upgrade'; |
| 107 | + proxy_set_header Host \$host; |
| 108 | + proxy_set_header X-Forwarded-Host \$host; |
| 109 | +
|
| 110 | + } |
| 111 | +
|
| 112 | +}""" | sudo tee $deploy_dir/nginx/sites-available/taxplorer |
| 113 | + |
| 114 | + |
| 115 | +if [ -f "/etc/nginx/sites-enabled/taxplorer" ]; then |
| 116 | + sudo mv /etc/nginx/sites-enabled/taxplorer /etc/nginx/sites-available/taxplorer.bak |
| 117 | +fi |
| 118 | +sudo cp $deploy_dir/nginx/sites-available/taxplorer /etc/nginx/sites-enabled/taxplorer |
| 119 | + |
| 120 | +# Restart Nginx to apply the changes |
| 121 | +sudo systemctl restart nginx |
0 commit comments