python(django)をapache2表示させたい(docker/ubuntu:18.04使用)
python3.6(django)でスクレイピングアプリを作っています。
使用しているものはdocker,ubuntu:18.04,apache2,python3.6.9,mod-wsgi
です。
python mamage.py runserver 0.0.0.0:8000
では問題なく動作確認は済んでいるのですが、apache2でのwebサーバーの元で動作させようと試みているのですが、「it works!」画面のままdjangoで作成した画面が表示されないです。
どうしたら、python(django)の画面を表示できるかご教授願います。
発生している問題・エラーメッセージ
$ sudo cat /var/log/apache2/error.log [Wed Aug 18 15:23:03.140584 2021] [mpm_event:notice] [pid 51:tid 139703595051968] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.9.0 Python/3.6 configured -- resuming normal operations [Wed Aug 18 15:23:03.140756 2021] [core:notice] [pid 51:tid 139703595051968] AH00094: Command line: '/usr/sbin/apache2'
該当のソースコード
wsgi.conf
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost WSGIScriptAlias / /var/www/html/my_app/Personal_development/Personal_development/wsgi.py WSGIDaemonProcess django python-home=/usr python-path=/var/www/html/my_app/Personal_development <Directory /var/www/html/my_app/Personal_development/Personal_development/> <Files wsgi.py> Require all granted </Files> </Directory> # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
dockerfile
FROM ubuntu:18.04 ENV PYTHONUNBUFFERED 1 ENV LANG="en_US.utf8" ENV LC_ALL="en_US.UTF-8" ENV LC_LANG="en_US.UTF-8" WORKDIR /var/www/html/ RUN set -x && \ apt update && \ apt upgrade -y #パッケージのダウンロード RUN set -x && \ apt-get install -y sudo \ vim \ wget \ python3 \ python3-pip \ init \ systemd \ apache2 \ apache2-dev \ libapache2-mod-wsgi \ gcc \ libmariadb-dev \ unzip \ language-pack-ja RUN localedef -f UTF-8 -i en_US en_US.UTF-8 # google-chrome RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add && \ echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list && \ apt-get update && \ apt-get install -y google-chrome-stable #pythonのバージョン切替(デフォルトだと2.7系が使われているため) RUN sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1 # # インストールしたChromeとPythonのchromedriver-binaryのバージョンが合わない場合があるので、 # google-chromeのバージョン情報から バージョンの近いものを pip installする # RUN google-chrome --version | perl -pe 's/([^0-9]+)([0-9]+.[0-9]+).+/$2/g' > chrome-version RUN pip3 install chromedriver-binary~=`cat chrome-version` && rm chrome-version #requtirements.txtに書かれているモジュールをインストール ADD requirements.txt /var/www/html/ RUN pip3 install -r requirements.txt #apacheの設定 ADD wsgi.conf /etc/apache2/sites-available/000-default.conf ADD wsgi.load /etc/apache2/mods-available/wsgi.load # RUN sudo a2ensite wsgi RUN echo "ServerName localhost" | tee /etc/apache2/conf-available/fqdn.conf RUN a2enconf fqdn CMD /sbin/init # RUN sudo /etc/init.d/apache2 start
docker-compose.yml
version: "3" volumes: django-db: services: db: image: mysql:5.7 container_name: mysql networks: - django-net volumes: - django-db:/var/lib/mysql - ./db/mysql/initdb.d:/docker-entrypoint-initdb.d - ./db/mysql/my.cnf:/etc/my.cnf ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: Personal_development MYSQL_USER: django MYSQL_PASSWORD: django TZ: Asia/Tokyo restart: always django-app: depends_on: - db build: . container_name: app networks: - django-net ports: - "8000:8000" volumes: - ./www:/var/www/html tty: true environment: DJANGO_DB_HOST: db DJANGO_DB_NAME: Personal_development DJANGO_DB_USER: django DJANGO_DB_PASSWORD: django privileged: true command: /sbin/init networks: django-net: volumes: django-db:
wsgi.py
""" WSGI config for Personal_development project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ """ import os import sys from django.core.wsgi import get_wsgi_application FILE_PATH = os.path.dirname(__file__) PROJECT_NAME = os.path.basename(FILE_PATH) sys.path.append(os.path.dirname(FILE_PATH)) sys.path.append(FILE_PATH) os.environ.setdefault("DJANGO_SETTINGS_MODULE", PROJECT_NAME + ".settings") # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Personal_development.settings') application = get_wsgi_application()
※その他のコードが気になるようでしたら、githubで公開しています。
https://github.com/yohei4/Django-Scraping
試したこと
systemctl status apache2実行後エラーはないように思えます。
$ systemctl status apache2 ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Wed 2021-08-18 15:23:03 UTC; 18s ago Process: 35 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 51 (apache2) Tasks: 73 (limit: 2225) CGroup: /docker/bc31493bc2c55c10f159e8b7935922785ce7cb7dc0302a4317a45c80a309faae/system.slice/apache2.service ├─51 /usr/sbin/apache2 -k start ├─52 /usr/sbin/apache2 -k start ├─53 /usr/sbin/apache2 -k start └─54 /usr/sbin/apache2 -k start Aug 18 15:23:03 bc31493bc2c5 systemd[1]: Starting The Apache HTTP Server... Aug 18 15:23:03 bc31493bc2c5 systemd[1]: Started The Apache HTTP Server.
補足情報(FW/ツールのバージョンなど)
python/3.6.9
Apache/2.4.29 (Ubuntu)
ubuntu/18.04
※その他のコードが気になるようでしたら、githubで公開しています。
https://github.com/yohei4/Django-Scraping
あなたの回答
tips
プレビュー