Skip to content

alexfinger21/carbonApp

Repository files navigation

Greenskies flask server

(NOTE) This closely resembles the server setup from the digital ocean tutorial

Setting up the user

RESTART THE SERVER

  • To restart the server - run the commands

        sudo systemctl restart <app_name>
        sudo systemctl restart nginx
    
  • To set up the user, create a new user by calling adduser <username>

  • Next, modify the permissions of the user by calling usermod -a -G sudo <username> (this adds the sudo group to the user)

  • To check if the change was successful, use an editor like nano to view the /etc/group file. You should see sudo:x:27:(other users),<username> If that's not what you see, make sure to edit the file accordingly

  • If you followed the steps correctly, the user should have sudo permissions.

Setting up flask and gunicorn

  • Firstly, make sure you're logged in the user you created above by running the command sudo -u <user> bash
  • Next, download the github repo in your home directory. In order to be able to git pull successfully, make sure to run the ssh-keygen command and copy id_rsa.pub key (which can be found in the .ssh directory of the home directory of the user) into your github settings page. Then, you will be able to clone the repo by using the code dropdown --> SSH
  • To install python and pip, run the following commands:
        sudo apt install python3-venv
        sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
    
  • To set up the virtual environment, run python3 -m venv <env_name> to install, and source <env_name>/bin/activate to enable
  • Then, run the following commands to install the necessary dependencies:
        sudo pip3 install -r requirements.txt
        pip install wheel
        pip install gunicorn flask
    
  • Here are some tests you can run to make sure everything is working (make sure firewall is disabled for this test)
        gunicorn --bind 0.0.0.0:8080  wsgi:app
    
  • Now you can stop the gunicorn by using ctrl+c and stop the venv by using the command deactivate

setting up nginx

  • Firstly, install install nginx by running sudo apt install nginx
  • Now, create the service file in /etc/systemd/system/<app_name>.service to run the gunicorn file. These should be the contents (appn name is the github repo name):
    [Unit]
    Description=Gunicorn instance to serve <app_name>
    After=network.target
    
    [Service]
    User=<user>
    Group=www-data
    WorkingDirectory=/home/<user>/<app_name>
    Environment="PATH=/home/<user>/<app_name>/<env_name>/bin"
    ExecStart=/home/<user>/<app_name>/<env_name>/bin/gunicorn --workers 3 --bind unix:<app_name>.sock -m 007 wsgi:app
    
    [Install]
    WantedBy=multi-user.target
    
  • To start the gunicorn service, run sudo systemctl start <app_name>.service (.service is optional)
  • To enable the service, execute sudo systemctl enable <app_name> and run sudo systemctl status <app_name> to make sure everything is running correctly. This is the message you should see (where greenskies-flask-app is your app_name):
        greenskies-flask-app.service - Gunicorn instance to serve greenskies-flask-app
     Loaded: loaded (/etc/systemd/system/greenskies-flask-app.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-08-04 17:01:30 UTC; 1 day 23h ago
    Main PID: 51867 (gunicorn)
      Tasks: 4 (limit: 9368)
     Memory: 142.3M
        CPU: 55.807s
     CGroup: /system.slice/greenskies-flask-app.service
             ├─51867 /usr/bin/python3 /home/greensky/greenskies-flask-app/greenskiesenv/bin/gunicorn --workers 3 --bind unix:gre>
             ├─51868 /usr/bin/python3 /home/greensky/greenskies-flask-app/greenskiesenv/bin/gunicorn --workers 3 --bind unix
    
  • Now that your gunicorn service is running, use an editor like nano to create the file /etc/nginx/sites-available/<app_name>. These should be the contents:
        server {
    listen 80;
    server_name <your_domain> <www.your_domain>;
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/<user>/<app_name>/<app_name>.sock;
    }
    

} ```

  • Now, execute this command to create a link between your project and sites-enabled: sudo ln -s /etc/nginx/sites-available/<app_name> /etc/nginx/sites-enabled

  • Make sure nginx has the correct permissions by executing sudo chmod 755 /home/<user>

  • Use sudo nginx -t to check for any syntax errors

  • In order to get everything working correctly, give the www-data group the correct permissions by executing sudo chown -R greensky:www-data greensky

  • Now run

        sudo systemctl start nginx
        sudo systemctl enable nginx
    
  • If you encounter any errors, trying checking the following:

        sudo less /var/log/nginx/error.log: checks the Nginx error logs.
        sudo less /var/log/nginx/access.log: checks the Nginx access logs.
        sudo journalctl -u nginx: checks the Nginx process logs.
        sudo journalctl -u myproject: checks your Flask app’s Gunicorn logs.
    
  • To start nginx and sudo systemctl enable nginx to make sure everything is working correctly. Follow the next steps to ensure that the firewall functions correctly

Setting up the firewall

  • To enable the firewall, execute these commands
    sudo ufw enable
    sudo ufw allow 22
    sudo ufw allow mysql
    sudo ufw status
    sudo ufw allow 3306
    sudo ufw allow 33060
    sudo ufw allow 'Nginx Full'

Testing

  • To test, simply make sure your environment (.venv) is enabled and run the following command: python -m testing.[test-name]

Gulp

  • We use gulp to automate tasks like minifying.

  • To install gulp, simply type npm i

  • The gulp commands are:

  • gulp buildAll - builds everything

  • gulp buildHTML - builds HTML

  • gulp buildJS - builds Javascript

  • gulp buildCSS - builds the CSS into one file (greensky.css)

  • gulp - default task; run this in order for gulp to watch changes to your code and automatically construct the build directory

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors