项目作者: Nilesh-Meharkar

项目描述 :
Django - Apache, Gunicorn and Nginx on cloud
高级语言:
项目地址: git://github.com/Nilesh-Meharkar/vps.git
创建时间: 2021-03-28T13:49:45Z
项目社区:https://github.com/Nilesh-Meharkar/vps

开源协议:

下载


Cloud server

  1. Create a cloud server machine.
  2. Create a user.

    sudo adduser nilesh

  3. Give sudo access to user.

    sudo usermod -aG sudo nilesh

Deploy django on apache.

  1. > sudo apt-get update
  2. > sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
  3. > sudo pip3 install virtualenv

Copy Code location

  1. > mkdir /var/www/workspace
  2. > copy django code in location `/var/www/workspace`
  3. > create virtual environment

Collect static

  1. > pip install -r vps/sample/req.txt
  2. > python manage.py collectstatic

Apache

  1. > sudo vim /etc/apache2/sites-available/000-default.conf
  • we can give any name to file.

We can change port by changing

Need to add Listen as well on top.
Need to add on top.

  1. Listen 8000
  2. <VirtualHost *:8000>
Sample config for port 80
  1. <VirtualHost *:80>
  2. ServerAdmin nilshmeharkar43@gmail.com
  3. #DocumentRoot /var/www/workspace
  4. <Directory /var/www/workspace/vps/sample/sample>
  5. <Files wsgi.py>
  6. Require all granted
  7. </Files>
  8. </Directory>
  9. WSGIDaemonProcess myproject python-home=/var/www/workspace/venv python-path=/var/www/workspace/vps/sample
  10. WSGIProcessGroup myproject
  11. WSGIScriptAlias / /var/www/workspace/vps/sample/sample/wsgi.py
  12. ErrorLog ${APACHE_LOG_DIR}/error.log
  13. CustomLog ${APACHE_LOG_DIR}/access.log combined
  14. </VirtualHost>

python-home -> Show environment location.

python-path -> Show Project location.
wsgi file location.

Apache commands

  1. Enable the site.

    sudo a2ensite 000-default.conf

  2. Disable the site.

    sudo a2dissite 000-default.conf

  3. Check apache error logs.

    sudo tail -f 15 /var/log/apache2/error.log

  4. Reload the server.

    sudo systemctl reload apache2

Extra

Run django app on gunicorn

  1. > pip install gunicorn
  2. > cd /var/www/workspace/vps/sample
  3. > gunicorn --bind 0.0.0.0:8500 sample.wsgi

Ngnix

  1. > sudo apt install nginx
  2. > sudo ufw app list
  3. > sudo ufw allow 'Nginx Full'

>

  1. > sudo vim /etc/nginx/sites-available/sample
Sample config for port 9000
  1. server {
  2. listen 9000;
  3. server_name 206.81.7.153;
  4. location = /favicon.ico { access_log off; log_not_found off; }
  5. location /static/ {
  6. root /var/www/workspace/vps/sample;
  7. }
  8. location / {
  9. include proxy_params;
  10. proxy_pass http://206.81.7.153;
  11. }
  12. }

>

  1. > sudo ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled
  2. > sudo systemctl restart nginx
  3. > sudo systemctl reload nginx
Check errors
  1. > sudo tail -f 15 /var/log/nginx/error.log