質問するログイン新規登録

質問編集履歴

1

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

2019/06/04 17:36

投稿

komatt
komatt

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,8 +1,7 @@
1
- ### 前提・実現したいこと
1
+ coreserver(https://cp.coreserver.jp/)
2
2
 
3
- ここに質問の内容を詳しく書いてください。
4
- coreserver(https://cp.coreserver.jp/)
5
3
  および
4
+
6
5
  さくらレンタルサーバースタンダートプラン(https://www.sakura.ne.jp)
7
6
 
8
7
  にて
@@ -29,12 +28,12 @@
29
28
 
30
29
  ~/public_html/ここ
31
30
 
32
- ここと書かれている部分を
31
+ ここ と書かれている部分を
33
- hogehoge.comのrootディレクトリにcoreserverの設定画面で設定
32
+ hogehoge.comのrootディレクトリにcoreserverの設定画面で設定.
33
+
34
34
  試しに
35
35
 
36
- ~/public_html/
36
+ ~/public_html/index.html
37
- └── index.html
38
37
 
39
38
  でhello world して
40
39
  hogehoge.com に飛んだらhello worldしたのでディレクトリ階層はちゃんと設定できていると思われる。
@@ -45,13 +44,13 @@
45
44
 
46
45
  して
47
46
 
48
- mystic
47
+ mysite
49
48
  ├── manage.py
50
49
  └── mysite
51
- ├── __init__.py
50
+ ,,,,├── __init__.py
52
- ├── settings.py
51
+ ,,,,├── settings.py
53
- ├── urls.py
52
+ ,,,,├── urls.py
54
- └── wsgi.py
53
+ ,,,,└── wsgi.py
55
54
 
56
55
  を作って [https://qiita.com/twinoze/items/5bd62a2bc08fee7596f1] の通りに hello world 出来るアプリを作り
57
56
 
@@ -61,18 +60,18 @@
61
60
 
62
61
  ちゃんと hello world してました。
63
62
 
64
- ここから FTP接続してサイトに書いてある通りに設定し
63
+ ここから FTP接続してサイトに書いてある通りに設定し階層構造をこのように設定し
65
64
 
66
65
  ~/public_html/
67
- mysite
66
+ ,,,, mysite
68
- ├── manage.py
67
+ ,,,,,,,, ├── manage.py
69
- └── mysite
68
+ ,,,,,,,, └── mysite
70
- ├── __init__.py
69
+ ,,,,,,,, ├── __init__.py
71
- ├── settings.py
70
+ ,,,,,,,, ├── settings.py
72
- ├── urls.py
71
+ ,,,,,,,, ├── urls.py
73
- └── wsgi.py
72
+ ,,,,,,,, └── wsgi.py
74
- .htaccess
73
+ ,,,, .htaccess
75
- django.cgi
74
+ ,,,, django.cgi
76
75
 
77
76
  public-html には mysite .htaccess django.cgi の三つが入っている状態にし
78
77
 
@@ -81,30 +80,126 @@
81
80
  をしてdjango.cgiのパーミッションを変更
82
81
 
83
82
  .htaccess の内容
84
-
83
+ ```ここに言語を入力
85
84
  ewriteEngine on
86
85
  RewriteBase /
87
86
  RewriteCond %{REQUEST_FILENAME} !-f
88
87
  RewriteRule ^(.*)$ django.cgi/$1 [QSA,L]
89
88
 
89
+ ```
90
90
 
91
- django.cgi の内容(変更した箇所を上から抜粋)
91
+ django.cgi の内容
92
92
 
93
+ ```
93
94
  #!/virtual/hogehoge/.pyenv/shims/python
95
+ # encoding: utf-8
96
+ """
97
+ django-.cgi
94
98
 
99
+ A simple cgi script which uses the django WSGI to serve requests.
100
+
101
+ Code copy/pasted from PEP-0333 and then tweaked to serve django.
102
+ http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
103
+
104
+ This script assumes django is on your sys.path, and that your site code is at
105
+ /djangoproject/src. Copy this script into your cgi-bin directory (or do
106
+ whatever you need to to make a cgi script executable on your system), and then
107
+ update the paths at the bottom of this file to suit your site.
108
+
109
+ This is probably the slowest way to serve django pages, as the python
110
+ interpreter, the django code-base and your site code has to be loaded every
111
+ time a request is served. FCGI and mod_python solve this problem, use them if
112
+ you can.
113
+
114
+ In order to speed things up it may be worth experimenting with running
115
+ uncompressed zips on the sys.path for django and the site code, as this can be
116
+ (theorectically) faster. See PEP-0273 (specifically Benchmarks).
117
+ http://www.python.org/dev/peps/pep-0273/
118
+
119
+ Make sure all python files are compiled in your code base. See
120
+ http://docs.python.org/lib/module-compileall.html
121
+
122
+ """
123
+
124
+ import os, sys
125
+
126
+ # Change this to the directory above your site code.
95
127
  # sys.path.append("/virtual/hogehoge/.pyenv/versions/3.7.3/lib/python3.7/site-packages")
96
128
  # sys.path.append("/virtual/public_html/mysite")
97
129
 
130
+ def run_with_cgi(application):
131
+
132
+ environ = dict(os.environ.items())
98
133
  environ['PATH_INFO'] = environ.get('PATH_INFO', "/")
134
+ environ['wsgi.input'] = sys.stdin.buffer
135
+ environ['wsgi.errors'] = sys.stderr.buffer
136
+ environ['wsgi.version'] = (1,0)
137
+ environ['wsgi.multithread'] = False
138
+ environ['wsgi.multiprocess'] = True
139
+ environ['wsgi.run_once'] = True
99
140
 
141
+ if environ.get('HTTPS','off') in ('on','1'):
142
+ environ['wsgi.url_scheme'] = 'https'
143
+ else:
144
+ environ['wsgi.url_scheme'] = 'http'
145
+
146
+ headers_set = []
147
+ headers_sent = []
148
+
149
+ def write(data):
150
+ if not headers_set:
151
+ raise AssertionError("write() before start_response()")
152
+
153
+ elif not headers_sent:
154
+ # Before the first output, send the stored headers
155
+ status, response_headers = headers_sent[:] = headers_set
156
+ sys.stdout.buffer.write(('Status: %s\r\n' % status).encode("ascii"))
157
+ for header in response_headers:
158
+ sys.stdout.buffer.write(('%s: %s\r\n' % header).encode("ascii"))
159
+ sys.stdout.buffer.write(('\r\n').encode("ascii"))
160
+
161
+ sys.stdout.buffer.write(data)
162
+ sys.stdout.buffer.flush()
163
+
164
+ def start_response(status,response_headers,exc_info=None):
165
+ if exc_info:
166
+ try:
167
+ if headers_sent:
168
+ # Re-raise original exception if headers sent
169
+ raise exc_info[0](exc_info[1]).with_traceback(exc_info[2])
170
+ finally:
171
+ exc_info = None # avoid dangling circular ref
172
+ elif headers_set:
173
+ raise AssertionError("Headers already set!")
174
+
175
+ headers_set[:] = [status,response_headers]
176
+ return write
177
+
178
+ result = application(environ, start_response)
179
+ try:
180
+ for data in result:
181
+ if data: # don't send headers until body appears
182
+ write(data)
183
+ if not headers_sent:
184
+ write('') # send headers now if body was empty
185
+ finally:
186
+ if hasattr(result,'close'):
187
+ result.close()
188
+
189
+ # Change to the name of your settings module
100
190
  os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
191
+ from django.core.wsgi import get_wsgi_application
192
+ run_with_cgi(get_wsgi_application())
101
193
 
194
+ コード
195
+ ```
102
196
 
103
197
  のように設定
104
198
 
105
199
  mysite内部の setting.pyの
106
-
200
+ ```ここに言語を入力
107
201
  ALLOWED_HOSTS = [‘hogehoge.com’]
202
+ ```
108
203
 
109
204
  と設定
110
205
 
@@ -115,15 +210,17 @@
115
210
  (root)ディレクトリに置いております。
116
211
 
117
212
  .bashrc の中身
118
-
213
+ ```ここに言語を入力
119
214
  export PYTHONPATH=/virtual/hogehoge/.pyenv/versions/3.7.3/lib/python3.7/site-packages
215
+ ```
120
216
 
121
217
  .bash_profile の中身
122
-
218
+ ```ここに言語を入力
123
219
  export PYENV_ROOT=$HOME/.pyenv
124
220
  export PATH=$PYENV_ROOT/bin:$PATH
125
221
  eval "$(pyenv init -)"
126
222
 
223
+ ```
127
224
  中身を全部消して、3回程度1から試したのですが、一向に 500 Internal Server Error から抜け出せません。
128
225
 
129
226
  抜けや書き込み足らずがあると思いますが指摘していただけましたら書き込みます。