gpt4 book ai didi

python - 如何在 Ubuntu 16+ 上使用 Apache 2.4 设置 Django 1.11 和 Python 3.6.1

转载 作者:行者123 更新时间:2023-11-30 22:34:05 26 4
gpt4 key购买 nike

我想知道如何在 Ubuntu 上快速简单地设置 Django

我读了太多的官方和用户文档,我的头晕目眩,但不 100% 确定从哪里开始。

我不是一个绝对的初学者,而且我对 Django、Python 和 Linux Shell 命令 (Ubuntu) 有一些应用知识,因此说明可以快速、切题。

注意:我在下面回答了我自己的问题...

最佳答案

在 Ubuntu 16+ 上使用 Apache 2.4 设置 Django 1.11 和 Python 3.6.1

Note: This does not cover everything you 'could do' - it's meant to get your server up and running with Django as quickly as possible. It should work for later versions, just remember to find the right packages and change versioning accordingly

You will likely have to change a lot of the paths included so be watchful.

<强>1。安装Python 3.6和virtualenv

sudo apt-get update
sudo apt-get install python3.6 python3.6-dev
sudo apt-get install virtualenv

( Ubuntu Packages https://packages.ubuntu.com/ )

<强>2。安装 Apache2 网络服务器

sudo apt-get install apache2 apache2-dev

( all things apache here https://httpd.apache.org/ )

<强>3。为您的项目创建并输入一个文件夹 - 然后在其中构建一个虚拟环境

mkdir ~/example.com
cd ~/example.com
virtualenv --python=/usr/bin/python3.6 py361ve

( more about virtual environments here https://virtualenv.pypa.io/en/stable/ )

<强>4。输入您的新虚拟环境以安装软件包

source py361ve/bin/activate

( using virtualenv https://virtualenv.pypa.io/en/stable/userguide/ )

<强>5。安装 Django、mod_wsgi 和任何其他需要的包

pip install django
pip install mod_wsgi
pip install ...

( no need for pip3 in virtual environment - pip uses your Virtual Environment python version here )

( more on pip can be found here https://pip.pypa.io/en/stable/ mod_wsgi https://modwsgi.readthedocs.io/en/develop/ )

<强>6。移动现有项目或在 ~/example.com 文件夹中创建新的 django 项目

django-admin startproject django_project

( how to build a Django app https://docs.djangoproject.com/en/1.11/intro/tutorial01/ )

<强>7。编辑 Django 项目文件夹中的 wsgi.py 文件以添加项目的 sys 路径

import sys 
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

Additional: sys.path.append(‘/path/to/your/library’)

( django deployment https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/ )

<强>8。编辑站点的 Django 项目的 settings.py 并设置项目正常运行所需的任何其他内容。

I put my ‘static’ and ‘media’ folders in a separate location from my python code ( within /var/www/example.com/ ) and I think you should too to prevent accidentally making your python code public.

(The VirtualHost example below the instructions should make the setup more clear)

( Django Settings https://docs.djangoproject.com/en/1.11/topics/settings/ )

<强>9。运行以下命令并复制输出以放置在 apache 配置文件中

mod_wsgi-express module-config

( https://pypi.python.org/pypi/mod_wsgi )

10。退出虚拟环境

deactivate

(You can re-enter your virtual environment any time using the source method in step 4)

11。导航到您的 apache2 配置文件夹(在 Ubuntu 上为/etc/apache2/)并将复制的文本放在 Apache2 配置文件的底部。

sudo nano apache2.conf

( more about apache2.conf [ or httpd.conf on some systems ] https://httpd.apache.org/docs/2.4/configuring.html )

12.导航到/etc/apache2/sites-available/并为您的站点创建一个新的 conf 文件。编辑 VirtualHost 模板(位于所有说明下方)并将其复制到此文件。

sudo nano example.com.conf

( VirtualHost examples https://httpd.apache.org/docs/2.4/vhosts/examples.html and mod_wsgi daemon mode https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-mode )

13.保存文件后,在 Apache 中启用此站点

sudo a2ensite example.com.conf
sudo service apache2 reload

( https://wiki.apache.org/httpd/DebianLikePlatform )

14。如果您想启用对站点的管理员访问权限,只需将文件从 python 虚拟环境复制到您的别名 static/admin 文件夹

example path: /py361ve/lib/python3.6/site-packages/django/contrib/admin/static/admin

( serving admin files https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/#serving-the-admin-files )

15。每当您更新任何 python 或 django 相关脚本时,您都需要“触摸”wsgi.py 文件来重新加载运行您站点的守护进程以使其处于事件状态,或者重新启动 apache...

touch ~/example.com/my_project/my_project/wsgi.py
or
sudo service apache2 restart

16。最后检查您的所有文件夹和文件是否具有足够的组(通常是 www-data )读写权限,并且数据库所在的文件夹(如果使用 sqlite)具有组写入权限。

( https://help.ubuntu.com/community/FilePermissions )

VirtualHost 示例模板...

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin info@example.com
DocumentRoot /var/www/example.com

<Directory /var/www/example.com>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Alias /robots.txt /var/www/example.com/robots.txt
Alias /favicon.ico /var/www/example.com/favicon.ico
Alias /static/ /var/www/example.com/static/
Alias /media/ /var/www/example.com/media/

<Directory /var/www/example.com/static>
Require all granted
</Directory>

<Directory /var/www/example.com/media>
Require all granted
</Directory>

WSGIDaemonProcess example.com python-home=/home/user_name/example.com/py361ve
WSGIProcessGroup example.com
WSGIScriptAlias / /home/user_name/example.com/django_project/django_project/wsgi.py

<Directory /home/user_name/example.com/django_project/django_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>

注意:请根据需要编辑错误并改进说明,但不要试图使事情过于复杂

关于python - 如何在 Ubuntu 16+ 上使用 Apache 2.4 设置 Django 1.11 和 Python 3.6.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com