質問編集履歴

1

設定内容及び試した結果を追記しました。

2023/05/17 11:24

投稿

Sylph
Sylph

スコア2

test CHANGED
File without changes
test CHANGED
@@ -7,7 +7,7 @@
7
7
  ### 発生している問題
8
8
  **仮想環境に入った状態でcurlコマンドを送ったところ、ページにエラーが表示されました。
9
9
  サーバー構築は初めてなので、皆さんの色々なアドバイスを頂ければと思います。**
10
- ```ubuntu
10
+ ```bash
11
11
  (djangovenv) root@hostname:/var/www/domain/html/djangovenv/project$ curl --unix-socket /run/gunicorn.sock localhost
12
12
  <html>
13
13
  <head>
@@ -17,6 +17,46 @@
17
17
  <h1><p>Internal Server Error</p></h1>
18
18
 
19
19
  </body>
20
+ </html>
21
+
22
+ # こちらは通っています
23
+ adminvps@hostname:~$ curl -I "http://localhost/"
24
+ HTTP/1.1 200 OK
25
+ Server: nginx/1.18.0 (Ubuntu)
26
+ Date: Tue, 16 May 2023 16:06:35 GMT
27
+ Content-Type: text/html
28
+ Content-Length: 612
29
+ Last-Modified: Fri, 21 Apr 2023 06:36:26 GMT
30
+ Connection: keep-alive
31
+ ETag: "64422eea-264"
32
+ Accept-Ranges: bytes
33
+
34
+ # こちらも通っています
35
+ adminvps@hostname:~$ curl localhost(もしくはhttp://サーバーIPアドレス/)
36
+ <!DOCTYPE html>
37
+ <html>
38
+ <head>
39
+ <title>Welcome to nginx!</title>
40
+ <style>
41
+ body {
42
+ width: 35em;
43
+ margin: 0 auto;
44
+ font-family: Tahoma, Verdana, Arial, sans-serif;
45
+ }
46
+ </style>
47
+ </head>
48
+ <body>
49
+ <h1>Welcome to nginx!</h1>
50
+ <p>If you see this page, the nginx web server is successfully installed and
51
+ working. Further configuration is required.</p>
52
+
53
+ <p>For online documentation and support please refer to
54
+ <a href="http://nginx.org/">nginx.org</a>.<br/>
55
+ Commercial support is available at
56
+ <a href="http://nginx.com/">nginx.com</a>.</p>
57
+
58
+ <p><em>Thank you for using nginx.</em></p>
59
+ </body>
20
60
  </html>
21
61
  ```
22
62
 
@@ -31,8 +71,8 @@
31
71
  [Service]
32
72
  User=adminvps
33
73
  Group=root
34
- WorkingDirectory=/var/www/domain/html/djangovenv/project
74
+ WorkingDirectory=/var/www/sample.com/html/djangovenv/project
35
- ExecStart=/var/www/domain/html/djangovenv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock sample.wsgi:application
75
+ ExecStart=/var/www/sample.com/html/djangovenv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock sample.wsgi:application
36
76
  [Install]
37
77
  WantedBy=multi-user.target
38
78
  ```
@@ -47,9 +87,9 @@
47
87
  WantedBy=sockets.target
48
88
  ```
49
89
  ### 参考3(サービスの状態確認)
50
- ```ubuntu
90
+ ```bash
51
91
  # アプリ名を「sample」としています
52
- (djangovenv) root@hostname:/var/www/domain/html/djangovenv/project# systemctl status sample
92
+ (djangovenv) root@hostname:/var/www/sample.com/html/djangovenv/project# systemctl status sample
53
93
  ● gunicorn.service - gunicorn daemon
54
94
  Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled)
55
95
  Active: active (running) since Sun 2023-05-14 04:09:21 JST; 4s ago
@@ -59,10 +99,10 @@
59
99
  Memory: 112.0M
60
100
  CPU: 1.148s
61
101
  CGroup: /system.slice/gunicorn.service
62
- ├─7419 /var/www/domain/html/djangovenv/bin/python3 /var/www/domain/html/djangovenv/bin/gunicorn --workers 3>
102
+ ├─7419 /var/www/sample.com/html/djangovenv/bin/python3 /var/www/sample.com/html/djangovenv/bin/gunicorn --workers 3>
63
- ├─7420 /var/www/domain/html/djangovenv/bin/python3 /var/www/domain/html/djangovenv/bin/gunicorn --workers 3>
103
+ ├─7420 /var/www/sample.com/html/djangovenv/bin/python3 /var/www/domain/html/djangovenv/bin/gunicorn --workers 3>
64
- ├─7421 /var/www/domain/html/djangovenv/bin/python3 /var/www/domain/html/djangovenv/bin/gunicorn --workers 3>
104
+ ├─7421 /var/www/sample.com/html/djangovenv/bin/python3 /var/www/sample.com/html/djangovenv/bin/gunicorn --workers 3>
65
- └─7422 /var/www/domain/html/djangovenv/bin/python3 /var/www/domain/html/djangovenv/bin/gunicorn --workers 3>
105
+ └─7422 /var/www/sample.com/html/djangovenv/bin/python3 /var/www/sample.com/html/djangovenv/bin/gunicorn --workers 3>
66
106
  May 14 04:09:21 hostname systemd[1]: Started gunicorn daemon.
67
107
  May 14 04:09:21 hostname gunicorn[7419]: [2023-05-14 04:09:21 +0900] [7419] [INFO] Starting gunicorn 20.1.0
68
108
  May 14 04:09:21 hostname gunicorn[7419]: [2023-05-14 04:09:21 +0900] [7419] [INFO] Listening at: unix:/run/gunicorn.sock (7419)
@@ -72,6 +112,161 @@
72
112
  May 14 04:09:21 hostname gunicorn[7422]: [2023-05-14 04:09:21 +0900] [7422] [INFO] Booting worker with pid: 7422
73
113
  lines 1-21/21 (END)
74
114
  ```
115
+
116
+ ### 参考3(settings.py)
117
+ ```py
118
+ from pathlib import Path
119
+ import os
120
+
121
+ BASE_DIR = Path(__file__).resolve().parent.parent
122
+ SECRET_KEY = 'django-insecure-○○=○○'
123
+ DEBUG = True
124
+ ALLOWED_HOSTS = ['localhost', 'サーバーIPアドレス', 'ドメイン名', 'www.ドメイン名']
125
+
126
+ LOGGING = {
127
+ 'version': 1,
128
+ 'disable_existing_loggers': False,
129
+ 'handlers': {
130
+ 'file': {
131
+ 'level': 'DEBUG',
132
+ 'class': 'logging.FileHandler',
133
+ 'filename': './app.log',
134
+ },
135
+ 'console': {
136
+ 'class': 'logging.StreamHandler',
137
+ },
138
+ },
139
+ 'loggers': {
140
+ 'django': {
141
+ 'handlers': ['file'],
142
+ 'level': 'DEBUG',
143
+ 'propagate': True,
144
+ },
145
+ },
146
+ }
147
+
148
+ INSTALLED_APPS = [
149
+ 'accounts.apps.AccountsConfig',
150
+ 'django.contrib.admin',
151
+ 'django.contrib.auth',
152
+ 'django.contrib.contenttypes',
153
+ 'django.contrib.sessions',
154
+ 'django.contrib.messages',
155
+ 'django.contrib.staticfiles',
156
+ 'bootstrap4',
157
+ ]
158
+
159
+ BOOTSTRAP4 = {
160
+ 'include_jquery': True,
161
+ }
162
+
163
+ MIDDLEWARE = [
164
+ 'django.middleware.security.SecurityMiddleware',
165
+ 'django.contrib.sessions.middleware.SessionMiddleware',
166
+ 'django.middleware.common.CommonMiddleware',
167
+ 'django.middleware.csrf.CsrfViewMiddleware',
168
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
169
+ 'django.contrib.messages.middleware.MessageMiddleware',
170
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
171
+ ]
172
+
173
+ ROOT_URLCONF = 'sample.urls'
174
+ TEMPLATES = [
175
+ {
176
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
177
+ 'DIRS': [os.path.join(BASE_DIR, 'templates')],
178
+ 'APP_DIRS': True,
179
+ 'OPTIONS': {
180
+ 'context_processors': [
181
+ 'django.template.context_processors.media',
182
+ 'django.template.context_processors.debug',
183
+ 'django.template.context_processors.request',
184
+ 'django.contrib.auth.context_processors.auth',
185
+ 'django.contrib.messages.context_processors.messages',
186
+ ],
187
+ 'builtins':[
188
+ 'bootstrap4.templatetags.bootstrap4',
189
+ ],
190
+ },
191
+ },
192
+ ]
193
+
194
+ WSGI_APPLICATION = 'sample.wsgi.application'
195
+ DATABASES = {
196
+ 'default': {
197
+ 'ENGINE': 'django.db.backends.mysql',
198
+ 'NAME': 'sample_db',
199
+ 'USER': 'root',
200
+ 'PASSWORD': '********',
201
+ 'HOST': 'localhost',
202
+ 'PORT': '3306',
203
+ 'OPTIONS': {
204
+ 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
205
+ },
206
+ }
207
+ }
208
+
209
+ PASSWORD_HASHERS = [
210
+ 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
211
+ 'django.contrib.auth.hashers.BCryptPasswordHasher',
212
+ 'django.contrib.auth.hashers.PBKDF2PasswordHasher',
213
+ 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
214
+ ]
215
+
216
+ AUTH_USER_MODEL = 'accounts.User'
217
+ AUTH_PASSWORD_VALIDATORS = [
218
+ {
219
+ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
220
+ },
221
+ {
222
+ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
223
+ },
224
+ {
225
+ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
226
+ },
227
+ {
228
+ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
229
+ },
230
+ ]
231
+
232
+ LANGUAGE_CODE = 'ja'
233
+ TIME_ZONE = 'Asia/Tokyo'
234
+ USE_I18N = True
235
+ USE_TZ = True
236
+ USE_L10N = False
237
+
238
+ from django.conf.global_settings import DATETIME_INPUT_FORMATS
239
+ DATETIME_INPUT_FORMATS += ('%Y/%m/%d',)
240
+ STATIC_URL = '/static/'
241
+ STATICFILES_DIRS = [
242
+ os.path.join(BASE_DIR, 'accounts/static'),
243
+ ]
244
+ STATIC_ROOT = '/var/www/sample.com/html/static_for_deploy'
245
+
246
+ # media root setting
247
+ MEDIA_URL = '/media/'
248
+ MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
249
+
250
+ LOGIN = '/accounts/login'
251
+ LOGIN_REDIRECT_URL = '/app'
252
+ LOGOUT_REDIRECT_URL = '/accounts/login'
253
+ ```
254
+ ### エラーの詳細
255
+ ```app.log
256
+ Invalid HTTP_HOST header: 'ドメイン名'. You may need to add 'ドメイン名' to ALLOWED_HOSTS.
257
+ Traceback (most recent call last):
258
+ File "/var/www/sample.com/html/djangovenv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
259
+ response = get_response(request)
260
+ File "/var/www/sample.com/html/djangovenv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__
261
+ response = self.process_request(request)
262
+ File "/var/www/sample.com/html/djangovenv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request
263
+ host = request.get_host()
264
+ File "/var/www/sample.com/html/djangovenv/lib/python3.10/site-packages/django/http/request.py", line 167, in get_host
265
+ raise DisallowedHost(msg)
266
+ django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'ドメイン名'. You may need to add 'ドメイン名' to ALLOWED_HOSTS.
267
+ ```
268
+ **↑「ALLOWED_HOSTS」は空にしていないのに何故でしょうか・・・**
269
+
75
270
  ### 補足情報(FW/ツールのバージョンなど)
76
271
  Ubuntu 22.04
77
272
  さくらのVPS