前提
- Djangoでブログアプリを作っています。
- 記事に画像を登録できるようにし、開発環境では問題なく機能しています。
- sakuraのVPSにデプロイし、webサーバーはapache2で本番環境を構築しています。
- 本番環境にて新規記事を投稿したところ、文字だけの記事は登録できたのですが、画像を登録しようとすると、ブラウザにエラー500が発生しました。
実現したいこと
- 画像付きの記事を登録できるようにしたい
発生している問題・エラーメッセージ
【ブラウザ】 Server Error (500) 【apache error.log】 root@ik1-413-38954:/Nippo/site/logs# cat error.log Exception ignored in: <function Local.__del__ at 0x7fbddb723d30> Traceback (most recent call last): File "/Nippo/venv/lib/python3.8/site-packages/asgiref/local.py", line 94, in __del__ NameError: name 'TypeError' is not defined ...省略
該当のソースコード
Ubutu20.04
1※apacheのconf 2 3(venv) root@ik1-413-38954:/etc/apache2/sites-available# cat Nippo.conf 4<VirtualHost *> 5 # The ServerName directive sets the request scheme, hostname and port that 6 # the server uses to identify itself. This is used when creating 7 # redirection URLs. In the context of virtual hosts, the ServerName 8 # specifies what hostname must appear in the request's Host: header to 9 # match this virtual host. For the default virtual host (this file) this 10 # value is not decisive as it is used as a last resort host regardless. 11 # However, you must set it for any further virtual host explicitly. 12 #ServerName www.example.com 13 14 ServerAdmin sample@example.com 15 DocumentRoot /Nippo 16 17 ErrorLog /Nippo/site/logs/error.log 18 CustomLog /Nippo/site/logs/access.log combined 19 20 alias /static /Nippo/site/public/static 21 <Directory /Nippo/site/public/static/> 22 Require all granted 23 </Directory> 24 25 alias /media /Nippo/site/public/media 26 <Directory /Nippo/site/public/media/> 27 Require all granted 28 </Directory> 29 30 <Directory /Nippo/src/main/> 31 <Files wsgi.py> 32 Require all granted 33 </Files> 34 </Directory> 35 36 WSGIDaemonProcess nippoapp python-path=/Nippo/src python-home=/Nippo/venv 37 WSGIProcessGroup nippoapp 38 WSGIScriptAlias / /Nippo/src/main/wsgi.py 39 40 # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 41 # error, crit, alert, emerg. 42 # It is also possible to configure the loglevel for particular 43 # modules, e.g. 44 #LogLevel info ssl:warn 45 46 #ErrorLog ${APACHE_LOG_DIR}/error.log 47 #CustomLog ${APACHE_LOG_DIR}/access.log combined 48 49 # For most configuration files from conf-available/, which are 50 # enabled or disabled at a global level, it is possible to 51 # include a line for only one particular virtual host. For example the 52 # following line enables the CGI configuration for this host only 53 # after it has been globally disabled with "a2disconf". 54 #Include conf-available/serve-cgi-bin.conf 55
Python3
1※Djangoのsettings 2 3(venv) root@ik1-413-38954:/Nippo/src/main/settings# cat base.py 4""" 5Django settings for main project. 6 7Generated by 'django-admin startproject' using Django 4.1.3. 8 9For more information on this file, see 10https://docs.djangoproject.com/en/4.1/topics/settings/ 11 12For the full list of settings and their values, see 13https://docs.djangoproject.com/en/4.1/ref/settings/ 14""" 15 16from pathlib import Path 17from django.urls import reverse_lazy 18 19# Build paths inside the project like this: BASE_DIR / 'subdir'. 20BASE_DIR = Path(__file__).resolve().parent.parent.parent 21PARENT_DIR = Path(__file__).resolve().parent.parent.parent.parent 22 23 24# Quick-start development settings - unsuitable for production 25# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 26 27# SECURITY WARNING: keep the secret key used in production secret! 28 29# SECURITY WARNING: don't run with debug turned on in production! 30DEBUG = False 31 32ALLOWED_HOSTS = ['***','localhost'] 33 34# Application definition 35 36INSTALLED_APPS = [ 37 'django.contrib.admin', 38 'django.contrib.auth', 39 'django.contrib.contenttypes', 40 'django.contrib.sessions', 41 'django.contrib.messages', 42 'django.contrib.staticfiles', 43 'django.contrib.sites', 44 'allauth', 45 'allauth.account', 46 'allauth.socialaccount', 47 'nippo', 48 'accounts', 49 'crispy_forms', 50 'bootstrap4', 51] 52 53MIDDLEWARE = [ 54 'django.middleware.security.SecurityMiddleware', 55 'django.contrib.sessions.middleware.SessionMiddleware', 56 'django.middleware.common.CommonMiddleware', 57 'django.middleware.csrf.CsrfViewMiddleware', 58 'django.contrib.auth.middleware.AuthenticationMiddleware', 59 'django.contrib.messages.middleware.MessageMiddleware', 60 'django.middleware.clickjacking.XFrameOptionsMiddleware', 61] 62 63ROOT_URLCONF = 'main.urls' 64 65TEMPLATES = [ 66 { 67 'BACKEND': 'django.template.backends.django.DjangoTemplates', 68 'DIRS': [ BASE_DIR / "templates" ], 69 'APP_DIRS': True, 70 'OPTIONS': { 71 'context_processors': [ 72 'django.template.context_processors.debug', 73 'django.template.context_processors.request', 74 'django.contrib.auth.context_processors.auth', 75 'django.contrib.messages.context_processors.messages', 76 'django.template.context_processors.media', 77 ], 78 }, 79 }, 80] 81 82WSGI_APPLICATION = 'main.wsgi.application' 83 84 85# Database 86# https://docs.djangoproject.com/en/4.1/ref/settings/#databases 87 88with open(f'{PARENT_DIR}/auth/***') as f: 89 *** = f.read().strip() 90 91with open(f'{PARENT_DIR}/auth/***') as f: 92 *** = f.read().strip() 93 94with open(f'{PARENT_DIR}/auth/***') as f: 95 *** = f.read().strip() 96 97DATABASES = { 98 'default': { 99 'ENGINE': 'django.db.backends.postgresql_psycopg2', 100 'NAME': ***, 101 'USER': ***, 102 'PASSWORD': ***, 103 'HOST': 'localhost', 104 'PORT': '5432', 105 } 106} 107 108# Password validation 109# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 110 111AUTH_PASSWORD_VALIDATORS = [ 112 { 113 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 114 }, 115 { 116 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 117 }, 118 { 119 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 120 }, 121 { 122 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 123 }, 124] 125 126 127# Internationalization 128# https://docs.djangoproject.com/en/4.1/topics/i18n/ 129 130LANGUAGE_CODE = 'ja' 131 132TIME_ZONE = 'Asia/Tokyo' 133 134USE_I18N = True 135 136USE_TZ = True 137 138 139# Static files (CSS, JavaScript, Images) 140# https://docs.djangoproject.com/en/4.1/howto/static-files/ 141 142STATIC_URL = '/static/' 143STATICFILES_DIRS = [BASE_DIR / "static_local" ] 144STATIC_ROOT = PARENT_DIR / 'site/public/static' 145 146MEDIA_URL = '/media/' 147MEDIA_ROOT = PARENT_DIR / "site/public/media" 148 149# Default primary key field type 150# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 151 152DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 153 154AUTH_USER_MODEL = 'accounts.User' 155 156AUTHENTICATION_BACKENDS = [ 157 'django.contrib.auth.backends.ModelBackend', 158 'allauth.account.auth_backends.AuthenticationBackend', 159] 160 161SITE_ID = 1 162 163ACCOUNT_USER_MODEL_USERNAME_FIELD = None 164ACCOUNT_USERNAME_REQUIRED = False 165ACCOUNT_AUTHENTICATION_METHOD = 'email' 166ACCOUNT_EMAIL_REQUIRED = True 167 168LOGIN_REDIRECT_URL = reverse_lazy('nippo-list') 169ACCOUNT_LOGOUT_REDIRECT_URL = reverse_lazy("account_login") 170 171ACCOUNT_EMAIL_VERIFICATION = "none" 172 173EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" 174 175CRISPY_TEMPLATE_PACK = 'bootstrap4' 176 177ACCOUNT_LOGOUT_ON_GET = True 178 179BOOTSTRAP4 = { 180 'include_jquery': True, 181}
VPSディレクトリ構成
1 Nippo ※VPSのトップディレクトリに置いています 2 ├── README.md 3 ├── auth 4 ├── requirements.txt 5 ├── site 6 │ ├── logs 7 │ └── public 8 │ ├── media 9 │ │ ├── pages 10│ │ └── images 11│ └── static 12├── src ※中身:accounts db.sqlite3 main manage.py media_local nippo requirements.txt static_local templates 13└── venv
試したこと
※各項目実施ごとにブラウザのキャッシュを削除して実行しました
1.apacheのconf書き換え
<VirtualHost *:80> ⇒ <VirtualHost *>
(参考サイト)https://forum.djangoproject.com/t/deploy-to-apache/5208/20
2.VPSで、group_name nippoapp、user_name root 登録
(参考サイト)https://www.monotalk.xyz/blog/centos-74-apache-python-%E9%80%A3%E6%90%BA%E6%99%82%E3%81%AB%E3%81%A4%E3%81%BE%E3%81%A5%E3%81%84%E3%81%9F%E3%81%A8%E3%81%93%E3%82%8D/
3.Pillowのインストール先の変更
(参考サイト)tps://teratail.com/questions/amrloy7ulp9v52
4.chmodユーザー権限設定
chmod g+x /Nippo
chmod g+x /Nippo/site
chmod g+x /Nippo/site/public
chmod g+x /Nippo/site/public/pages
chmod g+x /Nippo/site/public/images
補足情報(FW/ツールのバージョンなど)
(venv) root@ik1-413-38954:/Nippo/src# pip freeze
asgiref==3.6.0
backports.zoneinfo==0.2.1
bootstrap4==0.1.0
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==2.1.1
cryptography==38.0.4
defusedxml==0.7.1
Django==4.1.4
django-allauth==0.51.0
django-crispy-forms==1.14.0
ez-setup==0.9
idna==3.4
oauthlib==3.2.2
Pillow==9.3.0
psycopg2-binary==2.9.5
pycparser==2.21
PyJWT==2.6.0
python3-openid==3.2.0
requests==2.28.1
requests-oauthlib==1.3.1
sqlparse==0.4.3
urllib3==1.26.13
(venv) root@ik1-413-38954:/Nippo/src# python --version
Python 3.8.10
SakuraのVPS:Ubuntu 20.04
editor:Visual Studio Code バージョン: 1.74.2 (user setup)
コミット: e8a3071ea4344d9d48ef8a4df2c097372b0c5161
日付: 2022-12-20T10:29:14.590Z
Electron: 19.1.8
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Windows_NT x64 10.0.22621
Sandboxed: No
OS:エディション Windows 11 Pro
バージョン 22H2
インストール日 2022/11/06
OS ビルド 22621.963
エクスペリエンス Windows Feature Experience Pack 1000.22638.1000.0

あなたの回答
tips
プレビュー