我在CentOS 7上安装了我的Django 1.9.2项目,并且在此服务器上找不到请求的URL /错误。日志不提供任何nGinx错误。在我的带有manage.py runserver的PC上,它可以工作。
我如何在服务器上安装项目:
我正在使用此tutorial - 但我在我的环境中安装了python 3.4.3 - 在PyCharm中创建了一个项目名称的项目 - 使用我的项目从PyCharm通过FTP替换服务器上的项目
数据库工作(makemigration,migrate,collectstatic),site.com/admin正常工作,我正在获取位于全局模板文件夹中的自定义404.html页面。
DEBUG = False
ALLOWED_HOSTS = [
'*',
]
我的项目结构是:
我的全球网址:
from django.conf.urls import include, url
from django.contrib import admin
from bakot import urls as bakot_urls
from loginsys import urls as loginsys_urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(bakot_urls)),
url(r'^auth/', include(loginsys_urls)),
]
我的bakot网址:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from bakot import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^step(?P<step_id>[0-9]+)$', views.step, name='step'),
url(r'^question(?P<question_id>[0-9]+)$', views.question, name='question'),
url(r'^newpage(?P<step_id>[0-9]+)/$', views.new_page,name='newpage'),
url(r'^lastpage(?P<step_id>[0-9]+)/$', views.last_page,name='lastpage'),
url(r'^answerset$', views.answerset, name='answerset'),
url(r'^isperm$', views.isperm, name='isperm'),
]
我的loginsys网址:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from loginsys import views
urlpatterns = [
url(r'^login/', views.login, name='login'),
url(r'^logout/', views.logout, name='logout'),
url(r'^registration/', views.registration, name='registration'),
url('^', include('django.contrib.auth.urls'))
]
gunicorn:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=bakotiinii
Group=nginx
WorkingDirectory=/home/bakotiinii/apifolder
ExecStart=/home/bakotiinii/apifolder/djangoen/bin/gunicorn --workers 3 --bind unix:/home/bakotiinii/apifolder/apifolder.sock apifolder.wsgi:application
[Install]
WantedBy=multi-user.target
NGINX
sudo nano /etc/nginx/nginx.conf
server {
listen 80;
server_name example.ru www.example.ru;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/bakotiinii/apifolder;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/bakotiinii/apifolder/apifolder.sock;
}
}
索引
def index(request):
stepitem = Step.objects.all()
username = auth.get_user(request).username
start = Step.objects.get(id = 1)
argo = {
"stepitem" : stepitem,
"username" : username,
"start" : start,
}
return render(request, 'bakot/index.html', argo)
settings.py
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxx'
DEBUG = False
ALLOWED_HOSTS = [
'*',
]
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'xxx'
EMAIL_HOST_PASSWORD = 'xxx'
DEFAULT_FROM_EMAIL = 'xxx'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bakot',
'loginsys',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'apifolder.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media'
],
},
},
]
WSGI_APPLICATION = 'apifolder.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': 'localhost',
'PORT': '',
}
}
LANGUAGE_CODE = 'ru-ru'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (
)
我的项目中有2个应用程序。 bakot和loginsys,我也尝试将我的setting.py链接添加到他们的模板中,但没有任何成功。
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'bakot/templates/'),
os.path.join(BASE_DIR, 'loginsys/templates/'),
]
,