前提
ローカルで作ったDjangoのプロジェクトをVPSサーバーに保存したあと、gunicornとnginxを使ってVPSサーバーからDjangoのプロジェクトが起動しようとしていますが、立ち上がりません。
エラーメッセージに転記したターミナルのコメントのを見ると、どうもgunicornがワークしていないようです。
user名やパスワードはgunicornとDjangoのsettings.pyで同じものを指定しています。
実現したいこと
VPSサーバーからのDjangoプロジェクト起動できるようにしたいです。
発生している問題・エラーメッセージ
#ブラウザに表示されるエラーメッセージです。 ERROR The requested URL could not be retrieved The following error was encountered while trying to retrieve the URL: http://"VPSサーバーのアドレス"/"プロジェクトのpath"/ Connection to "VPSサーバーのアドレス" failed. The system returned: (110) Connection timed out The remote host or network may be down. Please try the request again. Your cache administrator is root. #gunicornのステータスメッセージです。 (todoenv) root@ik1-413-38954:~/todoproject# sudo systemctl status gunicorn × gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2022-09-05 21:32:25 JST; 2 days ago TriggeredBy: ● gunicorn.socket Process: 202174 ExecStart=/home/ryota/todoproject/todoenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock todoproject.wsgi:application (code=exited, status=217/USER) Main PID: 202174 (code=exited, status=217/USER) CPU: 1ms Sep 05 21:32:25 ik1-413-38954 systemd[1]: Started gunicorn daemon. Sep 05 21:32:25 ik1-413-38954 systemd[202174]: gunicorn.service: Failed to determine user credentials: No such process Sep 05 21:32:25 ik1-413-38954 systemd[202174]: gunicorn.service: Failed at step USER spawning /home/ryota/todoproject/todoenv/bin/gunicorn: No such process Sep 05 21:32:25 ik1-413-38954 systemd[1]: gunicorn.service: Main process exited, code=exited, status=217/USER Sep 05 21:32:25 ik1-413-38954 systemd[1]: gunicorn.service: Failed with result 'exit-code'. (todoenv) root@ik1-413-38954:~/todoproject#
該当のソースコード
ubuntu
1※基本的にUdemyの講座でベースとしているwebサイトの通りに入力しています。 2"How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04" 3https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04 4 5#pip、PostgreSQL、Nginxのインストール 6sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl 7 8#データベーステーブルの作成 9sudo -u postgres psql 10CREATE DATABASE myproject; 11CREATE USER myprojectuser WITH PASSWORD 'password'; 12ALTER ROLE myprojectuser SET client_encoding TO 'utf8'; 13ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed'; 14ALTER ROLE myprojectuser SET timezone TO 'UTC'; 15GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser; 16\q 17 18#リポジトリのclone 19#仮想環境の構築 20 21#Djangoとgnicornのインストール 22pip install django gunicorn psycopg2-binary 23 24#settings.pyの設定 25vim ~/todoproject/todoproject/settings.py 26ALLOWED_HOSTS = ['VPSのIP', ‘localhost’’] 27DATABASES = { 28 'default': { 29 'ENGINE': 'django.db.backends.postgresql_psycopg2', 30 'NAME': 'myproject', 31 'USER': 'myprojectuser', 32 'PASSWORD': 'password', 33 'HOST': 'localhost', 34 'PORT': '', 35 } 36} 37STATIC_ROOT = os.path.join(BASE_DIR, 'static/') 38:wq 39 40#migration等のコマンドの実行 41manage.py makemigrations 42manege.py migrate 43manage.py collectstatic 44manage.py createsuperuser 45 46#gunicornの起動確認 47sudo ufw allow 8000 48gunicorn --bind 0.0.0.0:8000 todoproject.wsgi 49 50#ブラウザでDjangoプロジェクトの確認 51'VPSのIP':8000/'projectのurl' 52 53※この段階で上記の"ブラウザに表示されるエラーメッセージ"が出てきます。 54 Udemyの講座でベースとしているwebサイトでいうとStep6の段階で先に進めなくなっています。 55
試したこと
1.'sudo -u postgres psql'を実行したときに、'could not change directory to "/root": Permission denied'と表示が出るのが気になったので、"PostgreSQL Bugs"というサイトのアドバイスをもとに以下を試しました。
https://granicus.if.org/pgbugs/16915
So, your problem is that you run sudo as the root user, and the root home
directory is more protected than other directories.
If you, for example run sudo -u postgres psql as another user, you will not
have the same (and only cosmetic) issue:
create user
useradd -m arne
give the user sudo rights
cat<<EOT >> /etc/sudoers
arne ALL=(ALL:ALL) NOPASSWD: ALL
EOT
root@c376851a8443:~# su - arne
$ sudo -u postgres psql
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type "help" for help.
postgres=#
Or, you just run psql as postgres without using sudo when you are root.
root@c376851a8443:~# su - postgres -c psql
psql (13.1 (Debian 13.1-1.pgdg100+1))
Type "help" for help.
postgres=#
Or, you just ignore the error message and just run sudo -u postgres psql.
In a Postgres 12 container:
docker run -d --rm --name=postgres_12 -e POSTGRES_PASSWORD=password
postgres:12
docker exec -it postgres_12 bash
root@cd3b256e5e0b:/# sudo -u postgres psql
psql (12.5 (Debian 12.5-1.pgdg100+1))
Type "help" for help.
postgres=#
2.SakuraのVPSではデフォルトでユーザーがubuntuになっているので、"【Linux】SSHでログイン後にsuでrootになれない主な原因3選"というサイトを参考にして、rootに変更してみましたが結果は変わりませんでした。
https://ses-blog.net/archives/tshoot/1286.html
補足情報(FW/ツールのバージョンなど)
VPS:さくらのVPS
Ubuntu:Ubuntu 20.04.4 LTS \n \l
IDE:PyCharm 2022.2.1 (Community Edition)
回答1件
あなたの回答
tips
プレビュー