Django Blog Part Two- Set up Nginx with Ubuntu

Django Blog Part Two- Set up Nginx with Ubuntu

I choose the AWS EC2 Ubuntu for my hosting server since ) I am more familiar with AWS? ec2. 2) I can get one-year free tier for t2small 3) There are more tutorials about how to host Nginx in EC2.

For the web server, I tried with appache2, but stuck with config.files and permissions, so i decided to use Nginx instead. It is easier, but there are several key components which we need to understand clearly such as Gunicorn,supervisor, otherwise it is hard to do troubleshooting.?

1. Set up the server in AWS

-get public IP, public domain, and private key

- ssh to the server: ssh -i { private key} ?username@public domain

- transfer files from local: scp -i ?{ private key }-r( recursive) username@public domain/location

2. Update the server

After log into the server, update first:

-sudo apt-get update

-sudo apt-get upgrade -y

3. Create virtual environment

Need to create a virtual environment for the project, what it does is to separate project environment with local host environment to make it easy to manage.

-install venv: sudo apt-get install python3-venv

-create venv called env: python3 -m venv env

-activate venv environment: source env/bin/activate

4.Install required libraries

Best practice is to pip freeze your environments in local project and put them in requirements.txt files with these commands:

?-collecting environments: pip freeze

- put them into requirements.txt files: pip freeze -> requirements.txt

-django: pip3 install django #?we need this to set up the django project

-nginx: sudo apt-get install -y nginx # this is the web server I used,

-gunicorn workers: pip3 install gunicorn # since nginx does not understand web application, we need something to translate the traffic which nginx received to the web application( jango application in this case), so web application can execute the right codes, and that is gunicorn.

-connect with wsgi: gunicorn --bind 0.0.0.0:8000 oskar_website.wsgi:application # via this, gunicorn locates wsgi and talk to the application with wsgi.

Now if you visit your site, you should be able to see the application, but when you stopped gunicorn, the application connection will be stopped, so here comes supervisor which helps keep the gunicorn running all the time.

-supervisor: sudo apt-get install -y supervisor # this is something very useful when you update the web application

5. Set up supervisor

Create a new file called gunicorn.conf: sudo touch gunicorn.conf

-set up supervisor config.file : cd /etc/supervisor/conf.d/ # this is the directory for the config

gunicorn.conf file edit:

[program:gunicorn] # name of the program

directory=/home/ubuntu/oskar_website # where your application file is

command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/oskar_website/app.sock oskar_website.wsgi:application # the command needs to run /

bind a socket which can read nginx, tell where the wsgi is (just need to point the location after directory)

autostart=true # whenever the server starts, auto start this program

autorestart=true

stderr_logfile=/var/log/gunicorn/gunicorn.err.log # error log location

stdout_logfile=/var/log/gunicorn/gunicorn.out.log

[group:guni] # group name is guni

programs:gunicorn

6. Enable the file:

-superviousr read the file:

-sudo supervisorctl reread # reread the config

-sudo supervisorctl update # tell the supervisour start the project

-sudo supervisorctl status

7. Read and enable the file

Next step is tell nginx to read from socket file which we defined above.

find config location: cd /etc/nginx/sites-available

create a new file and edit: sudo touch jango.conf

server{

listen 80;

server_name ?# domain name or IP address

location / {?# when someone visit the root location, it will direct to here

?????????include proxy_params;

?????????proxy_pass https://unix:/home/ubuntu/oskar_website/app.sock;

}

{

?????????location /static/ {

autoindex on;

alias /home/ubuntu/oskar_website/static;

}

}

}

-check the status of nginx: sudo nginx -t

-enable the config file: sudo ln django.conf /etc/nginx/sites-enabled/

-restart the nginx: sudo service nginx restart

8. Issues/Errors

1) While I am accessing my port gunicorn mysite.wsgi:application --bind=127.0.0.1:8001 in Nginx server, I am getting the following error in my error log file;2014/05/30 11:59:42 [crit] 4075#0: *6 connect () to 127.0.0.1:8001 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "https://127.0.0.1:8001/", host: "localhost:8080".Solution: ?https://django.fun/en/qa/146882/

2) static files cannot be rendered, solution: need to set up the static root in settings.py: STATIC_URL = os.path.join(BASE_DIR, 'static')

3) when I changed settings.py , it is not updated with the server,https://stackoverflow.com/questions/40711747/failed-to-start-gunicorn-service-unit-gunicorn-service-not-found/, ?solution: sudo supervisorctl reload, sudo systemctl reload nginx

4) sudo supervisorctl reload does not work , showing can not find gunicorn.service, solution:

create a .service file with-?sudo nano /etc/systemd/system/gunicorn.service, edit the file-Unit]

Description=gunicorn daemon

After=network.target

5) Pyhton version compatibility, better to keep the same python version in local and venv environment, for me, I have python 3.10 in local, and python 3.8 in venv because i need this version for backports.zoneinfo package, and it brings me some errors when i try to run the application.?

6) After the site runs, the form such as log in/sign up shows error, solution: /need to add trusted origin in settings.py, CSRF_TRUSTED_ORIGINS = ['https://wwww.oskarcode.com'], https://stackoverflow.com/questions/70285834/forbidden-403-csrf-verification-failed-request-aborted-reason-given-for-fail

Video link to refer:https://www.youtube.com/watch?v=_TBw7ALJp0Y

Summary: If this is your first time to configure web server, I will suggest 1) take a deep breath, because you might have a lot of errors, troubles which needs a great patient 2) take a break and come back in between key steps, good luck.

Oskar Ablimit

Cloudflare Enterprise Account Executive | Engineering in Cloud Computing |@ex-AWS| @ex-Alicloud | @ex-Tencent Cloud

2 年

Just added a new funny password generator to my site, check it out if you are struggling to find a matched password for complicated password requirements, https://www.oskarcode.com/passwordgenerator/

回复

要查看或添加评论,请登录

Oskar Ablimit的更多文章

社区洞察

其他会员也浏览了