将Django部署到服务器

3
这是我修改过的问题,原问题是如何成功地将Django应用程序部署到服务器上,因为这是我第一次这样做。以下链接是我尝试过的:
  1. https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
  2. https://simpleisbetterthancomplex.com/tutorial/2016/10/14/how-to-deploy-to-digital-ocean.html
  3. https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04
  4. https://www.linode.com/docs/web-servers/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04
  5. http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
我严格按照这些做了,一路跟到了 ~/myproject/manage.py runserver 0.0.0.0:8000 这一步,但当他们展示导航到服务器IP地址和端口8000时显示一个 'It worked' 页面时,我没有得到任何响应。
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

通过uwsgi --http :8000 --wsgi-file test.py,但仍然没有响应。在我苦思冥想了一段时间后,我决定放弃并跳过那一部分,直接将NGINX,uWSGI和我的Django应用程序结合起来。我刚刚设法让它工作,所以我会在下面发布一个详细的解决方案,说明我是如何做到这一点的,希望可以帮助其他人,因为我发现互联网上的大部分指导都没有解释一些需要解释才能充分理解正在发生的事情的内容。这将足以作为问题和答案...... 尽管附注一下,我仍然对于为什么在没有安装Web服务器的情况下我仍无法访问使用runserver命令运行的Django应用程序感兴趣,如果您知道,请评论。

鉴于您已经尝试了两种完全不同的方法——通过开发服务器访问Django和通过uwsgi访问简单的WSGI应用程序——但两者都无法正常工作,这表明问题很可能出在您的服务器上。(显然,问题不在数据库或静态文件上,因为您的第二次尝试根本没有使用Django。) - Daniel Roseman
3个回答

1

经过一段时间的思考,我成功地将我的Django应用程序部署到托管在Digitalocean上的服务器上,使用了NGINX和uWSGI。我不会在这里发布我的实际服务器IP,因此在此答案中,我将使用10.11.12.13作为我的Ubuntu服务器的IP地址。我尽力回溯了我的步骤,以下是按时间顺序排列的:

  1. Created a droplet on Digitalocean with Ubuntu 16.04.3 x64 with no 'additional options', then by email received the password for the root login:
  2. Log into the new server and setup a new password
    ➜ ssh root@10.11.12.13
    Update the system:
    # sudo apt-get update
  3. Add a new user jupiar and give it superuser privilege
    # sudo adduser jupiar && usermod -aG sudo jupiar
  4. Switch to user jupiar
    # su - jupiar
  5. Install NGINX webserver
    $ sudo apt-get install nginx
  6. I do a lot with data science, and my Django app uses alot of these features, so I prefer to work with Anaconda python as it's more complete from the start, so here download and install Anaconda python:
    $ wget https://repo.continuum.io/archive/Anaconda3-5.0.0.1-Linux-x86_64.sh
    $ bash Anaconda3-5.0.0.1-Linux-x86_64.sh
    Noting that in the installation, Anaconda3 was installed to /home/jupiar/anaconda3, and i chose [yes] to have the installer prepend the install location to PATH in the .bashrc
  7. Now to restart bash:
    $ . .bashrc
    $ source .bashrc
  8. Now I created a python virtual environment, and then activated it:
    $ conda create -n drfprojectenv python=3.6 anaconda
    $ source activate drfprojectenv
  9. Now install Django and then start a new project
    (drfprojectenv)$ pip install Django
    (drfprojectenv)$ django-admin.py startproject drfproject
  10. Setup the database:
    (drfprojectenv)$ cd drfproject
    (drfprojectenv)~/drfproject$ ./manage.py migrate
  11. Set a static directory:
    (drfprojectenv)~/drfproject$ echo 'STATIC_ROOT = os.path.join(BASE_DIR, "static/")' >> drfproject/settings.py
  12. Collect the static files:
    (drfprojectenv)~/drfproject$ ./manage.py collectstatic
  13. Now to install uWSGI, we need a few packages for the operating system so as to compile C code, for which uWSGI needs:
    (drfprojectenv)~/drfproject$ sudo apt-get install build-essential python-dev python3-dev
    (drfprojectenv)~/drfproject$ cd
    (drfprojectenv)$ pip install uwsgi
  14. Okay, now is where it got a little more interesting: We now need to create an initialization file (.ini) for our Django project:
    (drfprojectenv)$ sudo mkdir -p /etc/uwsgi/sites
    (The -p just ables us to make all necessary parent directories)
    (drfprojectenv)$ sudo nano /etc/uwsgi/sites/drfproject.ini
    And inside that file place the following:

    [uwsgi]
    project = drfproject
    base = /home/jupiar
    
    chdir = %(base)/%(project)
    home = %(base)/Env/%(project)
    module = %(project).wsgi:application
    master = true processes = 2
    socket = %(base)/%(project)/%(project).sock chmod-socket = 666 vacuum = true
  15. Now we need to make a uWSGI service, we can do that by:
    (drfprojectenv)$ sudo nano /etc/systemd/system/uwsgi.service
    and then inside that file place:

    [Unit]
    Description=uWSGI Emperor service
    After=syslog.target
    [Service] ExecStart=/home/jupiar/anaconda3/envs/drfprojectenv/bin/uwsgi --emperor /etc/uwsgi/sites/drfproject.ini Restart=always KillSignal=SIGQUIT Type=notify StandardError=syslog NotifyAccess=all
    [Install] WantedBy=multi-user.target
  16. Now, we want uWSGI to start automatically on a system restart, and activate the service:
    (drfprojectenv)$ sudo systemctl uwsgi enable
    (drfprojectenv)$ sudo systemctl uwsgi start
    Noting, that you may see:
    Warning: emperor.uwsgi.service changed on disk. Run 'systemctl daemon-reload' to reload units.
    Where we would just run:
    (drfprojectenv)$ sudo systemctl daemon-reload

  17. Check the service is running properly with:
    (drfprojectenv)$ sudo systemctl status uwsgi.service
    Where we should see something like:

    uwsgi.service - uWSGI Emperor service
        Loaded: loaded (/etc/systemd/system/uwsgi.service; enabled; vendor preset: enabled)
        Active: active (running)
    
    1. Start the service with this command too:
      (drfprojectenv)$ sudo service uwsgi start
    2. Now we need to configure NGINX:
      (drfprojectenv)$ sudo nano /etc/nginx/sites-available/default
      and then place the following
    server {
        listen 80;
        server_name _;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/drfproject;
    }
    
    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/jupiar/drfproject/drfproject.sock;
    }
    

    }
    1. Make sure that Django's ALLOWED_HOSTS have some combination of the following ip addresses:
      ALLOWED_HOSTS = ['10.11.12.13', '*', 'localhost', '127.0.0.1']

    2. Now stop and start uWSGI service, and restart nginx:
      (drfprojectenv)$ sudo service uwsgi stop
      (drfprojectenv)$ sudo service uwsgi start
      (drfprojectenv)$ sudo service nginx restart

    3. And that should be roughly it, all the steps I could backtrace are there. One thing you may need to do aswell is to install Django and uwsgi on the global installation of Anaconda, as well as within the virtual environment.
      Now that's installed properly, should be pretty effortless to switch it over to the rest framework.


0

在服务器上部署Django项目,您必须按照以下步骤逐步进行:

0#备份您的项目文件

1#通过服务器面板创建Python应用程序。

(如果您的服务器面板是cPanel,请查找“设置Python应用程序”)

2#在服务器上的public_html目录中的'file manager'上传'static''media'文件。

3#在面板服务器的'file manager'中查找名称为Python应用程序的目录,将项目文件上传到其中。

4#编辑您项目的settings.py文件:

DEBUG = FALSE

5#编辑您项目的settings.py文件:

ALLOWED_HOSTS = [ 'yourdomain' , 'www.yourdomain' , 'https://...' , ]

6# 在您的项目中编辑settings.py文件:

'media''static'目录的寻址方式从'os.path'更改为这些文件夹在服务器上的实际地址,例如:

STATIC_ROOT = '/home/username/app_name/static'

MEDIA_ROOT = '/home/username/app_name/media'

[ 这些步骤是通用的,对于不同的服务器,您可能需要进行其他设置。 ]


0

让我们从在DigitalOcean上配置您的droplet开始。您使用哪种Linux版本(如Ubuntu或Debian等)和哪种Web Server(如Nginx或Apache2等)?

我几个月前在DigitalOcean上设置了一个Debian droplet,自己安装了Apache Web服务器,现在一切都很顺利。我总是在我的项目库中安装虚拟环境,并使用pip安装我需要的所有内容。因此,我建议回到这种安排,而不是将其放在项目外部。一旦我有了自己的架构,设置了我的settings.py文件,我的wsgi.py文件,并收集了我的静态文件等,然后我使用迁移等来配置我的数据库。最后,我配置我的Web服务器以找到项目的位置。在apache2中,您需要在/etc/apache2/sites-available/中配置一个.conf文件,然后使用sudo a2ensite .conf在sites-enabled中启用它。在nginx中也会有类似的设置,您可能比我更了解它,因为您过去部署过Angular应用程序。因此,请检查这些重要步骤,看看是否有任何遗漏。


我正在使用Ubuntu 16.04.3,在我的问题中,我甚至还没有安装nginx或任何Web服务器,我尝试了三次Python,其中一次是在项目中使用virtualenvs,一次是不使用virtualenvs,甚至还使用了Anacoda的virtualenvs。现在我已经安装了nginx并尝试配置uwsgi,但是在使用ExecStart时出现了203/Exec错误。 - alexexchanges

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接