Skip to content

Instantly share code, notes, and snippets.

@nim4n136
Last active March 30, 2020 03:56
Show Gist options
  • Save nim4n136/68a536b1c88aee3d66c92342a3b1c631 to your computer and use it in GitHub Desktop.
Save nim4n136/68a536b1c88aee3d66c92342a3b1c631 to your computer and use it in GitHub Desktop.
Cara deploy flask dengan uwsgi centos

Cara deploy flask dengan uwsgi centos

  1. Buat file wsgi.py di folder project root flask dengan is file dengan code berikut:

    from myproject import app
    
    if __name__ == "__main__":
        app.run()
  2. Buat file config ini uwsg contoh dengan nama file project-flask.ini di dalam folder project root flask dengan isi file berikut

    [uwsgi]
    module = wsgi:app
    
    master = true
    processes = 5
    
    socket = project-running.sock
    chmod-socket = 660
    vacuum = true
    
    die-on-term = true
  3. Buat file service untuk start/stop flask di contoh dengan nama file project-flask.service di dalam folder /etc/systemd/system/ Contoh:

    sudo vi /etc/systemd/system/project-flask.service
    

    Dengan isi file

     [Unit]
     Description=uWSGI Server flask
     After=network.target
    
     [Service]
     User=apache
     Group=apache
     WorkingDirectory=/home/sysop/project-flask
     Environment="PATH=/home/sysop/virtualenv/bin"
     ExecStart=/home/sysop/virtualenv/bin/uwsgi --ini project-flask.ini
    
     [Install]
     WantedBy=multi-user.target
  4. Selanjutnya start service uWsgi

    Untuk start service atau menjalankan service

    sudo systemctl start project-flask
    

    Untuk mendaftarkan service supaya setiap kali server di restart service auto run

    sudo systemctl enable project-flask
    

    Untuk melihat service sudah running atau tidak

    sudo systemctl status project-flask
    

    NOTE: Nama project-flask adalah nama dari nama file project-flask.service yang sudah di buat sebelumnya

  5. Buat proxy uwsgi ke web server nginx sudo vi /etc/nginx/conf.d/project-flask.conf Dengan isi file

    server {
         listen 80;
         server_name ALAMAT_IP;
    
         location / {
             include uwsgi_params;
             uwsgi_pass unix:/home/sysop/project-flask/project-running.sock;
         }
     }

    NOTE: ALAMAT_IP di ganti dengan alamat IP SERVER atau DOMAIN SERVER

  6. Restart web server nginx

    sudo systemctl status nginx
    

    Note: Jika tidak ada error, silahkan buka aplikasi di web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment