gunicorn systemd启动未在流浪者箱中与django一起工作

gunicorn systemd启动未在流浪者箱中与django一起工作,django,ubuntu,nginx,chef-infra,gunicorn,Django,Ubuntu,Nginx,Chef Infra,Gunicorn,我把教程翻译成厨师的食谱。到目前为止,除了启动gunicorn(正确的方式)之外,一切似乎都在运行 例如,当我在通过vagrant-halt进行初始设置和设置后关闭机器,然后使用vagrant-up再次启动机器时,我总是会收到502坏网关错误。 然后,我必须ssh到该框中,手动运行这些命令 sudo systemctl后台程序重新加载 sudo systemctl重启gunicorn 在那之后,一切又恢复正常了 我不明白的是,当我在重新加载守护进程并重新启动gunicorn之前运行sudo sy

我把教程翻译成厨师的食谱。到目前为止,除了启动gunicorn(正确的方式)之外,一切似乎都在运行

例如,当我在通过
vagrant-halt
进行初始设置和设置后关闭机器,然后使用
vagrant-up
再次启动机器时,我总是会收到
502坏网关
错误。
然后,我必须ssh到该框中,手动运行这些命令

sudo systemctl后台程序重新加载

sudo systemctl重启gunicorn

在那之后,一切又恢复正常了

我不明白的是,当我在重新加载守护进程并重新启动gunicorn之前运行sudo systemctl status gunicorn时,它告诉我gunicorn正在运行

这是我的
gunicorn.service
文件内容,可以写入
/etc/systemd/sytem/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/vagrant_data
ExecStart=/vagrant_data/myprojectenv/bin/gunicorn --workers 5 --bind unix:/home/ubuntu/run/myproject.sock myproject.wsgi:application --reload

[Install]
WantedBy=multi-user.target
我的项目文件夹结构是:

/home/ubuntu/myproject ls
   manage.py myproject myprojectenv

/home/ubuntu/run ls
   myproject.sock
我将
myproject
文件夹符号链接到
vagrant\u数据
,该数据被设置为我的vagrant文件中的
vm.synced\u文件夹

这一切都运行在一个
ubuntu/xenial64
vagrant框中

更新:

include_recipe 'locale'

include_recipe 'apt'

execute 'install requirements' do
    command 'sudo apt-get install -y python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx'
    not_if ('sudo dpkg -l | grep postgresql')
end

bash 'setup database and user' do
    user 'postgres'
    code <<-EOF
        echo "CREATE DATABASE #{node['dbname']};" | psql
        echo "CREATE USER #{node['dbuser']} WITH PASSWORD '#{node['dbpass']}';" | psql
        echo "ALTER ROLE #{node['dbuser']} SET client_encoding TO 'utf8';" | psql
        echo "ALTER ROLE #{node['dbuser']} SET default_transaction_isolation TO 'read committed';" | psql
        echo "ALTER ROLE #{node['dbuser']} SET timezone TO 'UTC';" | psql
        echo "GRANT ALL PRIVILEGES ON DATABASE #{node['dbname']} TO #{node['dbuser']};" | psql
    EOF
    not_if { `sudo -u postgres psql -tAc \"SELECT * FROM pg_database WHERE datname='#{node['dbname']}'\" | wc -l`.chomp == "1" }
end

execute 'install virtualenv' do
    command 'sudo pip3 install virtualenv'
    not_if ('sudo pip3 list | grep virtualenv')
end

link "/home/ubuntu/#{node['projectDir']}" do
  to '/vagrant_data'
  owner 'ubuntu'
  group 'www-data'
end

directory '/home/ubuntu/run' do
  owner 'ubuntu'
  group 'www-data'
  action :create
end

bash 'configure and install django' do
    code <<-EOF
        cd /home/ubuntu/#{node['projectDir']}
        virtualenv myprojectenv
        source myprojectenv/bin/activate
        pip install django gunicorn psycopg2
        django-admin.py startproject #{node['projectDir']} .
        deactivate
    EOF
    not_if { ::File::exists?("/home/ubuntu/#{node['projectDir']}/#{node['projectDir']}")}
end

###############
# Note : In development set workers to 1 which will reload the code after each request
# in production set it to cores x 2 + 1 ... which would mostly result in 5 workers
##########
template '/etc/systemd/system/gunicorn.service' do
    source 'gunicorn.erb'
    owner 'root'
    group 'root'
end

template '/etc/nginx/sites-available/myproject' do
    source 'test.erb'
    owner 'www-data'
    group 'www-data'
end

execute 'link to sites-enabled' do
    command 'sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled'
    not_if { ::File.symlink?('/etc/nginx/sites-enabled/myproject')}
end

execute 'remove default host' do
    command 'sudo rm /etc/nginx/sites-enabled/default'
    only_if { ::File.exists?('/etc/nginx/sites-enabled/default') }
end

bash 'enable gunicorn' do
    code <<-EOF
        sudo systemctl daemon-reload
        sudo systemctl start gunicorn
        sudo systemctl enable gunicorn
    EOF
    #not_if { ::File.exists?('/home/ubuntu/run/myproject.sock')}
end

execute 'test nginx' do
    command 'sudo nginx -t'
end

execute 'restart nginx' do
    command 'sudo service nginx restart'
end
这样,我就不必每次开机都给Wagrant打电话了


但我还是很想知道如何让事情以正确的方式开始

我疯狂地猜测这可能与事情完成的顺序有关。也许systemd会先启动gunicorn,然后挂载一些类似/vagrant_的目录数据?这会造成足够的混乱。你可能是对的,但据我所知,同步文件夹装载发生在设置之前。此外,.socket文件没有符号链接,它位于项目目录之外。如果没有看到您的实际配方代码,我们所说的一切都只是猜测。如果您想看到使用Chef部署Django应用程序的示例,请看OK-我将用配方代码更新我的答案。我认为另一个选项是通过.sh脚本启动gunicorn,如图所示-但我不是舒尔,因为还有很多选项和可能性,我猜测可能必须这样做按照事情完成的顺序去做。也许systemd会先启动gunicorn,然后挂载一些类似/vagrant_的目录数据?这会造成足够的混乱。你可能是对的,但据我所知,同步文件夹装载发生在设置之前。此外,.socket文件没有符号链接,它位于项目目录之外。如果没有看到您的实际配方代码,我们所说的一切都只是猜测。如果您想看到一个使用Chef部署Django应用程序的示例,请看OK-我将用配方代码更新我的答案。我认为另一个选项是通过此处所示的.sh脚本启动gunicorn-但我不是舒尔,因为还有很多选项和可能性
config.trigger.after :up do
   run_remote "sudo systemctl daemon-reload"
   run_remote "sudo systemctl restart gunicorn"
end