1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | エックスサーバでPythonによるWebサービスを行う手順について説明します。 ======================= 1,X-serverでサービスを取得する 2,brewをインストール 3,python3.8をインストール 4,Djangoをインストール 5,プロジェクトを作成 6,Webから接続 7,アプリを追加 8,AdminのCSSを有効にする ======================= 1,X-serverでサービスを取得する はじめにサーバIDを分かりやすい名前を決めて xxxxxxxxをサーバIDに置き換えてください chmod 600 xxxxxxxx.key ssh -l xxxxxxxx -i xxxxxxxx.key xxxxxxxx.xsrv.jp -p 10022 cat /etc/redhat-release python -V python3 -V 2,brewをインストール sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" echo 'eval $(/home/xxxxxxxx/.linuxbrew/bin/brew shellenv)' >>~/.profile eval $(/home/xxxxxxxx/.linuxbrew/bin/brew shellenv) source ~/.profile brew install gcc brew doctor python -V python3 -V 3,python3.8をインストール brew install python3 python -V python3 -V python3 -m venv env source env/bin/activate python -V 4,Djangopyをインストール pip install --upgrade pip pip install django==1.11.17 python -m django --version cd pwd 5,プロジェクトを作成 django-admin startproject mysite ls cd mysite python manage.py migrate python manage.py createsuperuser 6,Webから接続 cd cd xxxxxxxx.xsrv.jp cd public_html vi test.html < html > < hr > good!! < hr > </ html > mkdir top cd top ls /home/xxxxxxxx/.linuxbrew/bin/python* ※インストールされたpythonのバージョンにあわせ 以下の必要な場合はCGI内のパスを変更してください vi mysite.cgi i ------------ #!/home/xxxxxxxx/.linuxbrew/bin/python3.8 # encoding: utf-8 import os, sys sys.path.append("/home/xxxxxxxx/env/lib/python3.8/site-packages") sys.path.append("/home/xxxxxxxx/env/lib/python3.8/site-packages/django") sys.path.append("/home/xxxxxxxx/mysite/") def run_with_cgi(application): environ = dict(os.environ.items()) environ['wsgi.input'] = sys.stdin.buffer environ['wsgi.errors'] = sys.stderr.buffer environ['wsgi.version'] = (1,0) environ['wsgi.multithread'] = False environ['wsgi.multiprocess'] = True environ['wsgi.run_once'] = True environ['PATH_INFO'] = environ.get('PATH_INFO',"/") # 追加 if environ.get('HTTPS','off') in ('on','1'): environ['wsgi.url_scheme'] = 'https' else: environ['wsgi.url_scheme'] = 'http' headers_set = [] headers_sent = [] def write(data): if not headers_set: raise AssertionError("write() before start_response()") elif not headers_sent: # Before the first output, send the stored headers status, response_headers = headers_sent[:] = headers_set sys.stdout.buffer.write(('Status: %s\r\n' % status).encode("ascii")) for header in response_headers: sys.stdout.buffer.write(('%s: %s\r\n' % header).encode("ascii")) sys.stdout.buffer.write(('\r\n').encode("ascii")) sys.stdout.buffer.write(data) sys.stdout.buffer.flush() def start_response(status,response_headers,exc_info=None): if exc_info: try: if headers_sent: # Re-raise original exception if headers sent raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) finally: exc_info = None # avoid dangling circular ref elif headers_set: raise AssertionError("Headers already set!") headers_set[:] = [status,response_headers] return write result = application(environ, start_response) try: for data in result: if data: # don't send headers until body appears write(data) if not headers_sent: write('') # send headers now if body was empty finally: if hasattr(result,'close'): result.close() # Change to the name of your settings module os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.core.wsgi import get_wsgi_application run_with_cgi(get_wsgi_application()) ------- vi .htaccess RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ mysite.cgi/$1 [QSA,L] chmod 755 mysite.cgi chmod 604 .htaccess cd cd mysite cd mysite vi settings.py ALLOWED_HOSTS = [] ALLOWED_HOSTS = ['*'] 7,アプリを追加 cd ../ python manage.py startapp polls cd polls vi views.py from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.") --- vi urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ] cd ../ cd mysite vi urls.py from django.conf.urls import include url(r'^polls/', include('polls.urls', namespace='polls')), vi settings.py INSTALLED_APPS = [ 'polls', ] 8,AdminのCSSを有効にする vi settings.py # Static files (CSS, JavaScript, Images) #STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = './static/' vi urls.py ---- from django.conf import settings from django.conf.urls.static import static from django.views.static import serve urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^admin/([a-xA-Z0-9_]*/)*static/(?P< path >.*)$', serve, {'document_root': settings.STATIC_ROOT}), ] cd ../ python manage.py collectstatic vi settings.py LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'ja' 関連ページ |