Python 如何在不运行服务器的情况下设置django
我试图从远程服务器访问django网站。 我在Python 如何在不运行服务器的情况下设置django,python,django,Python,Django,我试图从远程服务器访问django网站。 我在settings.py中允许所有PC ALLOWED_HOSTS = ['*', 'localhost'] 然后运行python manage.py runserver 0.0.0.0:8085 我可以从连接的计算机访问该站点。但在访问站点之前,我需要在服务器系统上运行runservercommanad。有没有不运行命令就可以访问网站的方法 编辑:服务器是windows部署Django的方法很多,但我不知道一个简单的方法:-) 在所有方面,您至少需
settings.py中允许所有PC
ALLOWED_HOSTS = ['*', 'localhost']
然后运行python manage.py runserver 0.0.0.0:8085
我可以从连接的计算机访问该站点。但在访问站点之前,我需要在服务器系统上运行runserver
commanad。有没有不运行命令就可以访问网站的方法
编辑:服务器是windows部署Django的方法很多,但我不知道一个简单的方法:-)
在所有方面,您至少需要一件东西:Web服务器
常见的Web服务器是Apache2和nginx
我假设您使用的是基于debian的服务器(例如Ubuntu)
如果选择apache2,请使用以下设备安装apache:
sudo apt install apache2 libapache2-mod-wsgi-py3
创建包含主机信息的文件
/etc/apache2/sites available/foo.conf
<VirtualHost *:80>
# This is name based virtual hosting. So place an appropriate server name
# here. Example: django.devsrv.local
ServerName {{ project_name }}.com
ServerAlias www.{{ project_name }}.com
# This alias makes serving static files possible.
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
#Alias /static/ {{ project_directory }}/static_root/
#<Directory {{ project_directory }}/static_root>
# Options -Indexes
# Order deny,allow
# Allow from all
#</Directory>
# This alias makes serving media files possible.
# Please note, that this is geared to our settings/common.py
# In production environment, you will propably adjust this!
#Alias /media/ {{ project_directory }}/media/
#<Directory {{ project_directory }}/media>
# Options -Indexes
# Order deny,allow
# Allow from all
#</Directory>
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / {{ project_directory }}/{{ project_name }}/wsgi.py
# PROCESS_NAME specifies a distinct name of this process
# see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
# PATH/TO/PROJECT_ROOT is the full path to your project's root directory,
# containing your project files
# PATH/TO/VIRTUALENV/ROOT: If you are using a virtualenv specify the full
# path to its directory.
# Generally you must specify the path to Python's site-packages.
WSGIDaemonProcess {{ project_name }} threads=1 python-path={{ project_directory }}/:{{ path_to_virtualenv}}/lib/python{{ python_version }}/site-packages/
# PROCESS_GROUP specifies a distinct name for the process group
# see: https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup
WSGIProcessGroup {{ project_name }}
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/{{ project_name }}_error.log
CustomLog ${APACHE_LOG_DIR}/{{ project_name }}_access.log combined
</VirtualHost>
如果您的静态文件不工作,请确保在settings.py中正确配置了它:
STATIC\u URL='/STATIC/'
STATIC\u ROOT=os.path.join(BASE\u DIR,“STATIC\u ROOT”)
STATICFILES_DIRS=[os.path.join(BASE_DIR,“static”),]
然后编辑foo.conf并重新启动apache。runserver
只是开发服务器,有很多方法可以运行站点,如部署文档中所述。该文档似乎有点混乱。我是django的新手。是否有可供参考的资源这是资源。但还有数百个其他教程。我认为所有方法都需要使用commandscan启动服务器,我在windows中使用相同的方法?不完全相同,但应该可以工作。
sudo a2ensite foo.conf
sudo service apache2 restart