Code Monkey home page Code Monkey logo

server-configs's Introduction

Deploying django project to vps:

  • Prerequisites:
    • Create a requirements.txt file that contains all packages used in project:

      • pip freeze > requirements.txt
    • Set up static files:

      • Install whitenoise:
        • pip install whitenoise
        • pip freeze > requirements.txt
      • Paste the following at the top of middleware list:
        • "whitenoise.middleware.WhiteNoiseMiddleware",
      • Add the following:
        STATIC_URL = '/static/'
        STATIC_ROOT = BASE_DIR / "staticfiles"
        STATICFILES_DIRS = [
            os.path.join(BASE_DIR, 'static'),
        ]
        
        STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
      • Collect static files:
        • python manage.py collectstatic
    • Push to github repository

  1. Ssh into server:

    • run:
      • ssh username@server_ip_address
      • Enter password  
  2. Update & Upgrade Server:

    • sudo apt-get update
    • sudo apt-get upgrade  
  3. Install python & pip:

    • sudo apt install python3
    • sudo apt install python3-pip  
  4. Create & activate virtual environment:

    • virtualenv /opt/myproject
    • source /opt/myproject/bin/activate  
  5. Clone Repository:

    • cd /opt/myproject
    • mkdir myproject
      • It may seem redundant to have two directories with the same name; however, it makes it so that your virtualenv name and project name are the same.
    • cd myproject
    • check if git is installed:
      • git status
      • sudo apt-get install git
    • Clone repo: git clone repo-url
    • install requirements:
      • cd repo-name
      • pip install -r requirements.txt
      • pip install gunicorn  
  6. Nginx Configuration:

    • Install nginx:

      • sudo apt install nginx
    • sudo nano /etc/nginx/sites-available/myproject

    • Type the following into the file:

      server {
         listen 80;
         server_name yourip;
      
         access_log /var/log/nginx/website-name.log;
      
         location /static/ {
             alias /opt/myproject/myproject/path-to-static-files/;
         }
      
         location / {
             proxy_pass http://127.0.0.1:8000;
             proxy_set_header X-Forwarded-Host $server_name;
             proxy_set_header X-Real-IP $remote_addr;
             add_header P3P 'CP="ALLDSP COR PSAa PSDa OURNOR ONL UNI COM NAV"';
         }
      }
    • Now we need to set up a symbolic link in the /etc/nginx/sites-enabled directory that points to this configuration file. That is how NGINX knows this site is active. Change directories to /etc/nginx/sites-enabled like this:

      • cd /etc/nginx/sites-enabled
      • sudo ln -s ../sites-available/myproject
    • Go to:

      • /etc/nginx/nginx.conf
      • uncomment this line:
        • # server_names_hash_bucket_size 64;
    • Restart nginx:

      • sudo service nginx restart

     

  7. Adjusting the Firewall

    • Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.

    • sudo apt-get install ufw

    • sudo ufw allow 8000

    • Restart nginx:

      • sudo service nginx restart  
  8. Testing configuration:

    • Run gunicorn:
      • cd /opt/myproject/myproject/repo-name
      • gunicorn --bind 0.0.0.0:8000 project_name.wsgi
    • Visit your project on your server ip address on port 8000:
      • yourServerIp:8000

     

  9. Connecting domain:

    • open your domain registrar and open your dns settings for the specified domain

    • Add an A record with name @, pointing to the ip address of the server you are hosting your project on

    • Save changes

    • Open your nginx configuration file:

      • sudo nano /etc/nginx/sites-available/myproject
    • Make the following changes:

      server {
          listen 80;
          server_name yourdomain.com www.yourdomain.com;
      
          access_log /var/log/nginx/website-name.log;
      
          location /static/ {
              alias /opt/myproject/myproject/path-to-static-files/;
          }
      
          location / {
              proxy_pass yourServerIp:8000;
              proxy_set_header X-Forwarded-Host $server_name;
              proxy_set_header X-Real-IP $remote_addr;
              add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
          }
      }
    • Restart nginx:

      • sudo service nginx restart
    • Wait for dns changes to propagate and check your website on your connected domain  

  10. Install SSL on vps with Let's Encrypt (Requires domain):

    • sudo apt install certbot
    • sudo apt install certbot python3-certbot-nginx
    • run the following and follow the process:
      • sudo certbot --nginx -d your_domain.com
    • check nginx configuration:
      • sudo nginx -t
    • reload nginx: sudo systemctl reload nginx
    • run gunicorn:
      • gunicorn --bind 0.0.0.0:8000 project_name.wsgi
  • Run gunicorn in the background: nohup gunicorn --bind 0.0.0.0:8000 project_name.wsgi &  

  • kill gunicorn running in background:

    • pkill gunicorn  
  • Making changes

Refrences

Articles:

server-configs's People

Contributors

deji12 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.