cd /etc/rc.d/init.d/
sudo vim ***autostart.sh
1 #!/bin/sh
2 #chkconfig: 2345 81 96
3 #description: Starttomcat
4 /usr/local/bin/uwsgi /srv/www/E3_Rack_site/uwsgi.ini
#(分别是uwsgi的绝对路径和uwsgi.ini的绝对路径,另uwsgi.ini为配置项目deploy时生成并编写)
#后附deploy部分笔记
#增加刚编写文件的可执行权限
sudo chmod a+x /etc/rc.d/init.d/***autostart.sh

#设置并添加到开机自启动项中
sudo chkconfig --add ***autostart.sh
sudo chkconfig ***autostart.sh on

 

0) put project folder into /srv/www/

1) install uwsgi

    $ sudo pip3 install uwsgi

2) install nginx
    
    $ sudo yum install nginx.x86_64

    $ sudo systemctl enable nginx.service    // Add nginx to daemon-reload list

3) close selinux if it is opened

    $ sudo vim /etc/selinux/config

    modified from,

    ...
    SELINUX=enforcing
    ...

    to

    ...
    SELINUX=disabled
    ...

    check with,
    $ sestatus
    SELinux status:             disabled

4) modify django project settings

    1> settings.py
    ==============

    ...

    # DEBUG = True
    DEBUG = False

    # ALLOWED_HOSTS = []
    ALLOWED_HOSTS = ['localhost', '10.217.32.107']  # allowed server ip address

    ...

    # For deploy static files
    STATIC_ROOT = os.path.join(BASE_DIR, 'BurnIn_Static_Collection')


    2> Collecte all static files into STATIC_ROOT 
    =============================================

    open project with vscode,
    open terminal and run,
    (Note, after any static file changed, need to run this command again, the collected static files cannot update automatically)

    $ python3 manage.py collectstatic

    then all static files collected into STATIC_ROOT


    3> Base urls.py
    ===============

    ...

    # For deploy static
    from django.views import static
    from django.conf.urls import url
    from django.conf import settings


    urlpatterns = [
        ...

        # For deploy static
        url('^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static'),
    ]


5) try uwsgi

    1> if use sqlite3 & CentOS7, due to the default sqlite3 is 3.7.17, so need to modify the Django lib,

    /usr/local/lib64/python3.6/site-packages/django/db/backends/sqlite3/base.py

    From,

    ...
    def check_sqlite_version():
        if Database.sqlite_version_info < (3, 8, 3):
            raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)
    ...

    To,

    ...
    def check_sqlite_version():
        if Database.sqlite_version_info < (3, 7, 3):
            raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)
    ...


    2> then, test if uwsgi works, 
    
    $ uwsgi --http :1492 --chdir /srv/www/BurnIn_Web_Application/ -w BurnIn.wsgi
      [ --master --processes 4 --threads 2 ]  # This will spawn 4 processes (each process with 2 threads), a master process (will re-spawn your 
                                              # processes when they die) and the HTTP router.
      [ --stats 127.0.0.1:9001]  # Note, with this option, you can install `$ pip3 install uwsgitop` then to monitor each processes load dynamically
                                 # with command `$ uwsgitop 127.0.0.1:9001`.

    open local browser address : http://localhost:1492/
    open remote browser address: http://10.217.32.107:1492/

    it works, Ctrl + C to terminate it.


    3> then, create uwsgi config file under current Web Application Root directory,

    uwsgi.ini
    ===============

    [uwsgi]
    # Web socket
    #socket=127.0.0.1:1491
    socket=localhost:1491

    # Web application base dirrectory
    chdir=/srv/www/BurnIn_Web_Application

    # Django wsgi module 
    #module=BurnIn.wsgi
    # Django wsgi file
    wsgi-file=BurnIn/wsgi.py

    # Maximum number of worker processes
    processes=4

    # Each process with threads
    # If you start uwsgi without threads, the Python GIL will not be enabled, so threads generated by
    # your application will never run. 
    # If you want to maintain Python threads support without starting multiple threads for your application,
    # just add the `--enable-threads` in command line or  `enable-threads=true` in ini config file.
    threads=2

    # Process 
    master=true

    #-------------------------------------

    # Buffer size (default is 4096 byte)
    buffer-size=65536

    # Clear environment on exit
    vacuum=true

    # Pid file
    pidfile=%(chdir)/uwsgi.pid

    # Status
    # stats=%(chdir)/uwsgi.status
    # stats=127.0.0.1:9001  # then, can use `$ telnet 127.0.0.1 9001` to get uwsgi server status, or use `uwsgitop localhost:9001`
    stats=localhost:9001 # then, can use `$ telnet localhost 9001` to get uwsgi server status, or use `uwsgitop localhost:9001`


    4> config the nginx server

    /etc/nginx/nginx.conf
    =====================

    chang from,

    server {
        ...
    }

    to,

    server {
        #listen       1492;
        #server_name  127.0.0.1;
        listen        localhost:1492;

        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        # Pass every request to the uwsgi server bound to port 1491, speaking the uwsgi protocol.
        location / {
            include uwsgi_params;
            #uwsgi_pass 127.0.0.1:1491;
            uwsgi_pass localhost:1491;
            uwsgi_read_timeout 2;
        }

        location /static/ {
            expires 30d;
            autoindex on;
            add_header Cache-Control private;
            alias /srv/www/BurnIn_Web_Application/BurnIn_Static_Collection/;
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

    then, after save the change, test the nginx config to make sure no syntax error,

    $ sudo nginx -t
    

    5> restart nginx server

    $ sudo systemctl restart nginx.service
    $ sudo systemctl status nginx.service  (optional, check nginx status)


    6> chang to Web Application root directory, start uwsgi server

    $ uwsgi [ --ini ] uwsgi.ini


    7> then,

    local address  : http://localhost:1492/
    remote address : http://10.217.32.107:1492/


    #===================================================================================== (uwsgi other operations)

    Check uwsgi detail status,

    $ uwsgi --connect-and-read ../BurnIn_Web_Application/uwsgi.status 

    Reload uwsgi,

    $ uwsgi --reload ../BurnIn_Web_Application/uwsgi.pid

    Stop uwsgi,

    $ uwsgi --stop ../BurnIn_Web_Application/uwsgi.pid