如何在Ubuntu 16+上使用Apache 2.4安装Django 1.11和Python 3.6.1

2024-09-29 04:25:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我读了太多的官方和用户文档,我的脑子都乱了,不知道从哪里开始。在

我不是一个绝对的初学者,我对Django、Python和linuxshell命令(Ubuntu)有一定的工作知识,所以这些指令可以很快且切中要害。在

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


Tags: django命令官方ubuntu指令用户文档初学者脑子
1条回答
网友
1楼 · 发布于 2024-09-29 04:25:27

在Ubuntu16+上使用Apache2.4使用Python3.6.1设置django1.11

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。安装Python3.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 Web服务器

^{pr2}$

( 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-admin startproject django_project

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

7。编辑wsgi.py在Django项目文件夹中添加文件,为项目添加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项目设置.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配置文件夹(/etc/apache2/在Ubuntu上),并将复制的文本放在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文件。编辑虚拟主机模板(在所有说明下方)并将其复制到此文件。

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数据)读写权限,以及数据库所在的文件夹(如果使用sqlite)是否具有组写入权限。

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

虚拟主机示例模板…

<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>

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

相关问题 更多 >