質問編集履歴

1

コードをわかりやすくした

2019/06/04 17:36

投稿

komatt
komatt

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,13 +1,11 @@
1
- ### 前提・実現したいこと
2
-
3
-
4
-
5
- ここに質問の内容を詳しく書いてください。
6
-
7
1
  coreserver(https://cp.coreserver.jp/)
8
2
 
3
+
4
+
9
5
  および
10
6
 
7
+
8
+
11
9
  さくらレンタルサーバースタンダートプラン(https://www.sakura.ne.jp)
12
10
 
13
11
 
@@ -60,93 +58,93 @@
60
58
 
61
59
 
62
60
 
63
- ここと書かれている部分を
61
+ ここ と書かれている部分を
64
-
62
+
65
- hogehoge.comのrootディレクトリにcoreserverの設定画面で設定
63
+ hogehoge.comのrootディレクトリにcoreserverの設定画面で設定.
64
+
65
+
66
66
 
67
67
  試しに
68
68
 
69
69
 
70
70
 
71
+ ~/public_html/index.html
72
+
73
+
74
+
75
+ でhello world して
76
+
77
+ hogehoge.com に飛んだらhello worldしたのでディレクトリ階層はちゃんと設定できていると思われる。
78
+
79
+
80
+
81
+ 次にローカルにて
82
+
83
+
84
+
85
+ django-admin startproject mysite
86
+
87
+
88
+
89
+ して
90
+
91
+
92
+
93
+ mysite
94
+
95
+ ├── manage.py
96
+
97
+ └── mysite
98
+
99
+ ,,,,├── __init__.py
100
+
101
+ ,,,,├── settings.py
102
+
103
+ ,,,,├── urls.py
104
+
105
+ ,,,,└── wsgi.py
106
+
107
+
108
+
109
+ を作って [https://qiita.com/twinoze/items/5bd62a2bc08fee7596f1] の通りに hello world 出来るアプリを作り
110
+
111
+
112
+
113
+ python manage.py runserver してローカルで
114
+
115
+ http://127.0.0.1:8000
116
+
117
+ を立ち上げて hello world 出来ているかを確認。
118
+
119
+
120
+
121
+ ちゃんと hello world してました。
122
+
123
+
124
+
125
+ ここから FTP接続してサイトに書いてある通りに設定し階層構造をこのように設定し
126
+
127
+
128
+
71
129
  ~/public_html/
72
130
 
73
- └── index.html
74
-
75
-
76
-
77
- でhello world して
131
+ ,,,, mysite
78
-
132
+
79
- hogehoge.com に飛んだらhello worldしたのでディレクトリ階層はちゃんと設定できていると思われる。
133
+ ,,,,,,,, ├── manage.py
80
-
81
-
82
-
83
- 次にローカルにて
134
+
84
-
85
-
86
-
87
- django-admin startproject mysite
135
+ ,,,,,,,, └── mysite
88
-
89
-
90
-
91
- して
136
+
92
-
93
-
94
-
95
- mystic
96
-
97
- ├── manage.py
98
-
99
- └── mysite
100
-
101
- ├── __init__.py
137
+ ,,,,,,,, ├── __init__.py
102
-
138
+
103
- ├── settings.py
139
+ ,,,,,,,, ├── settings.py
104
-
105
- ├── urls.py
140
+
106
-
107
- └── wsgi.py
108
-
109
-
110
-
111
- を作って [https://qiita.com/twinoze/items/5bd62a2bc08fee7596f1] の通りに hello world 出来るアプリを作り
112
-
113
-
114
-
115
- python manage.py runserver してローカルで
141
+ ,,,,,,,, ├── urls.py
116
-
142
+
117
- http://127.0.0.1:8000
143
+ ,,,,,,,, └── wsgi.py
118
-
144
+
119
- を立ち上げて hello world 出来ているかを確認。
145
+ ,,,, .htaccess
120
-
121
-
122
-
123
- ちゃんと hello world してました。
146
+
124
-
125
-
126
-
127
- ここから FTP接続してサイトに書いてある通りに設定し
128
-
129
-
130
-
131
- ~/public_html/
132
-
133
- mysite
134
-
135
- ├── manage.py
136
-
137
- └── mysite
138
-
139
- ├── __init__.py
140
-
141
- ├── settings.py
142
-
143
- ├── urls.py
144
-
145
- └── wsgi.py
146
-
147
- .htaccess
148
-
149
- django.cgi
147
+ ,,,, django.cgi
150
148
 
151
149
 
152
150
 
@@ -164,7 +162,7 @@
164
162
 
165
163
  .htaccess の内容
166
164
 
167
-
165
+ ```ここに言語を入力
168
166
 
169
167
  ewriteEngine on
170
168
 
@@ -176,15 +174,81 @@
176
174
 
177
175
 
178
176
 
179
-
177
+ ```
180
-
178
+
179
+
180
+
181
- django.cgi の内容(変更した箇所を上から抜粋)
181
+ django.cgi の内容
182
+
183
+
184
+
182
-
185
+ ```
183
-
184
186
 
185
187
  #!/virtual/hogehoge/.pyenv/shims/python
186
188
 
187
-
189
+ # encoding: utf-8
190
+
191
+ """
192
+
193
+ django-.cgi
194
+
195
+
196
+
197
+ A simple cgi script which uses the django WSGI to serve requests.
198
+
199
+
200
+
201
+ Code copy/pasted from PEP-0333 and then tweaked to serve django.
202
+
203
+ http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
204
+
205
+
206
+
207
+ This script assumes django is on your sys.path, and that your site code is at
208
+
209
+ /djangoproject/src. Copy this script into your cgi-bin directory (or do
210
+
211
+ whatever you need to to make a cgi script executable on your system), and then
212
+
213
+ update the paths at the bottom of this file to suit your site.
214
+
215
+
216
+
217
+ This is probably the slowest way to serve django pages, as the python
218
+
219
+ interpreter, the django code-base and your site code has to be loaded every
220
+
221
+ time a request is served. FCGI and mod_python solve this problem, use them if
222
+
223
+ you can.
224
+
225
+
226
+
227
+ In order to speed things up it may be worth experimenting with running
228
+
229
+ uncompressed zips on the sys.path for django and the site code, as this can be
230
+
231
+ (theorectically) faster. See PEP-0273 (specifically Benchmarks).
232
+
233
+ http://www.python.org/dev/peps/pep-0273/
234
+
235
+
236
+
237
+ Make sure all python files are compiled in your code base. See
238
+
239
+ http://docs.python.org/lib/module-compileall.html
240
+
241
+
242
+
243
+ """
244
+
245
+
246
+
247
+ import os, sys
248
+
249
+
250
+
251
+ # Change this to the directory above your site code.
188
252
 
189
253
  # sys.path.append("/virtual/hogehoge/.pyenv/versions/3.7.3/lib/python3.7/site-packages")
190
254
 
@@ -192,13 +256,137 @@
192
256
 
193
257
 
194
258
 
259
+ def run_with_cgi(application):
260
+
261
+
262
+
263
+ environ = dict(os.environ.items())
264
+
195
265
  environ['PATH_INFO'] = environ.get('PATH_INFO', "/")
196
266
 
197
-
267
+ environ['wsgi.input'] = sys.stdin.buffer
268
+
269
+ environ['wsgi.errors'] = sys.stderr.buffer
270
+
271
+ environ['wsgi.version'] = (1,0)
272
+
273
+ environ['wsgi.multithread'] = False
274
+
275
+ environ['wsgi.multiprocess'] = True
276
+
277
+ environ['wsgi.run_once'] = True
278
+
279
+
280
+
281
+ if environ.get('HTTPS','off') in ('on','1'):
282
+
283
+ environ['wsgi.url_scheme'] = 'https'
284
+
285
+ else:
286
+
287
+ environ['wsgi.url_scheme'] = 'http'
288
+
289
+
290
+
291
+ headers_set = []
292
+
293
+ headers_sent = []
294
+
295
+
296
+
297
+ def write(data):
298
+
299
+ if not headers_set:
300
+
301
+ raise AssertionError("write() before start_response()")
302
+
303
+
304
+
305
+ elif not headers_sent:
306
+
307
+ # Before the first output, send the stored headers
308
+
309
+ status, response_headers = headers_sent[:] = headers_set
310
+
311
+ sys.stdout.buffer.write(('Status: %s\r\n' % status).encode("ascii"))
312
+
313
+ for header in response_headers:
314
+
315
+ sys.stdout.buffer.write(('%s: %s\r\n' % header).encode("ascii"))
316
+
317
+ sys.stdout.buffer.write(('\r\n').encode("ascii"))
318
+
319
+
320
+
321
+ sys.stdout.buffer.write(data)
322
+
323
+ sys.stdout.buffer.flush()
324
+
325
+
326
+
327
+ def start_response(status,response_headers,exc_info=None):
328
+
329
+ if exc_info:
330
+
331
+ try:
332
+
333
+ if headers_sent:
334
+
335
+ # Re-raise original exception if headers sent
336
+
337
+ raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
338
+
339
+ finally:
340
+
341
+ exc_info = None # avoid dangling circular ref
342
+
343
+ elif headers_set:
344
+
345
+ raise AssertionError("Headers already set!")
346
+
347
+
348
+
349
+ headers_set[:] = [status,response_headers]
350
+
351
+ return write
352
+
353
+
354
+
355
+ result = application(environ, start_response)
356
+
357
+ try:
358
+
359
+ for data in result:
360
+
361
+ if data: # don't send headers until body appears
362
+
363
+ write(data)
364
+
365
+ if not headers_sent:
366
+
367
+ write('') # send headers now if body was empty
368
+
369
+ finally:
370
+
371
+ if hasattr(result,'close'):
372
+
373
+ result.close()
374
+
375
+
376
+
377
+ # Change to the name of your settings module
198
378
 
199
379
  os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
200
380
 
201
-
381
+ from django.core.wsgi import get_wsgi_application
382
+
383
+ run_with_cgi(get_wsgi_application())
384
+
385
+
386
+
387
+ コード
388
+
389
+ ```
202
390
 
203
391
 
204
392
 
@@ -208,10 +396,12 @@
208
396
 
209
397
  mysite内部の setting.pyの
210
398
 
211
-
399
+ ```ここに言語を入力
212
400
 
213
401
  ALLOWED_HOSTS = [‘hogehoge.com’]
214
402
 
403
+ ```
404
+
215
405
 
216
406
 
217
407
  と設定
@@ -232,15 +422,17 @@
232
422
 
233
423
  .bashrc の中身
234
424
 
235
-
425
+ ```ここに言語を入力
236
426
 
237
427
  export PYTHONPATH=/virtual/hogehoge/.pyenv/versions/3.7.3/lib/python3.7/site-packages
238
428
 
429
+ ```
430
+
239
431
 
240
432
 
241
433
  .bash_profile の中身
242
434
 
243
-
435
+ ```ここに言語を入力
244
436
 
245
437
  export PYENV_ROOT=$HOME/.pyenv
246
438
 
@@ -250,6 +442,8 @@
250
442
 
251
443
 
252
444
 
445
+ ```
446
+
253
447
  中身を全部消して、3回程度1から試したのですが、一向に 500 Internal Server Error から抜け出せません。
254
448
 
255
449