From 4ceb1e4a0036a19b3a3729bf66162e16ce8f1e98 Mon Sep 17 00:00:00 2001 From: David McInnis Date: Thu, 27 Oct 2016 23:37:10 -0700 Subject: [PATCH] Docs/system d change (#1376) * expanded and clarified systemd deployment instructions * fixed typo * fixed typo * fixed my stuff going past the 80-char page-width --- docs/source/deploy.rst | 76 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/docs/source/deploy.rst b/docs/source/deploy.rst index 0fe260e1..59aa24bf 100644 --- a/docs/source/deploy.rst +++ b/docs/source/deploy.rst @@ -188,8 +188,8 @@ Another useful tool to monitor and control Gunicorn is Supervisor_. A Upstart ------- -Using Gunicorn with upstart is simple. In this example we will run the app "myapp" -from a virtualenv. All errors will go to /var/log/upstart/myapp.log. +Using Gunicorn with upstart is simple. In this example we will run the app +"myapp" from a virtualenv. All errors will go to /var/log/upstart/myapp.log. **/etc/init/myapp.conf**:: @@ -208,10 +208,12 @@ from a virtualenv. All errors will go to /var/log/upstart/myapp.log. Systemd ------- -A tool that is starting to be common on linux systems is Systemd_. Here -are configurations files to set the Gunicorn launch in systemd and -the interfaces on which Gunicorn will listen. The sockets will be managed by -systemd: +A tool that is starting to be common on linux systems is Systemd_. Below are +configurations files and instructions for using systemd to create a unix socket +for incoming Gunicorn requests. Systemd will listen on this socket and start +gunicorn automatically in response to traffic. Later in this section are +nstructions for configuring Nginx to forward web traffic to the newly created +unix socket: **/etc/systemd/system/gunicorn.service**:: @@ -225,7 +227,8 @@ systemd: User=someuser Group=someuser WorkingDirectory=/home/someuser/applicationroot - ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/pid --bind unix:/run/gunicorn/socket applicationname.wsgi + ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/pid \ + --bind unix:/run/gunicorn/socket applicationname.wsgi ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true @@ -240,15 +243,13 @@ systemd: [Socket] ListenStream=/run/gunicorn/socket - ListenStream=0.0.0.0:9000 - ListenStream=[::]:8000 [Install] WantedBy=sockets.target -**/usr/lib/tmpfiles.d/gunicorn.conf**:: +**/etc/tmpfiles.d/gunicorn.conf**:: - d /run/gunicorn 0755 someuser someuser - + d /run/gunicorn 0755 someuser somegroup - Next enable the services so they autostart at boot:: @@ -261,10 +262,57 @@ Either reboot, or start the services manually:: systemctl start gunicorn.socket -After running ``curl http://localhost:9000/``, Gunicorn should start and you -should see something like that in logs:: +After running ``curl --unix-socket /run/gunicorn/socket http:``, Gunicorn +should start and you should see something like that in logs:: + + 2013-02-19 23:48:19 [31436] [DEBUG] Socket activation sockets: + unix:/run/gunicorn/socket,http://0.0.0.0:9000,http://[::]:8000 + +If you are also using Nginx with Systemd make sure to pass incoming traffic to +the new Gunicorn socket. Below is the http section of my nginx.conf + +**/etc/tmpfiles.d/gunicorn.conf**:: + http { + include mime.types; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + disable_symlinks off; + + server { + listen 8000; + server_name 127.0.0.1; + + location /static/ { + alias /static/; + } + + location / { + proxy_set_header Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_pass http://unix:/run/gunicorn/socket; + } + } + + + +Now make sure you enable the nginx service so it automatically starts at boot: + + systemctl enable nginx.service + +Either reboot, or start Nginx with the following command: + + systemctl start nginx + +Now you should be able to test Nginx with Gunicorn by visiting +http://127.0.0.1:8000/ in any web browser. Please note that the listen and +server_name used here are configured for a local machine. In a production +server you will most likely listen on port 80, and use your URL as the +server_name. Apache or any other web-server can be used instead of Nginx. - 2013-02-19 23:48:19 [31436] [DEBUG] Socket activation sockets: unix:/run/gunicorn/socket,http://0.0.0.0:9000,http://[::]:8000 Logging =======