CentOS 環境下基於 Nginx uwsgi 搭建 Django 站點

由於個人我的網站 restran.net 已經啓用,博客園的內容已經再也不更新。請訪問個人我的網站獲取這篇文章的最新內容,CentOS 環境下基於 Nginx uwsgi 搭建 Django 站點javascript

如下用一個網站 ocean_monitor 舉例css

MySQL 安裝與配置

安裝

MariaDB is shipped in the CentOS repo as of CentOS 7 instead of mysql.
if you still want to install mysql you need to add mysql rpm dependency into your yum repo.html

sudo yum -y install mariadb-server mariadb-devel mariadb
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

配置

登陸java

mysql -u root -p

建立用戶python

CREATE USER ocean_monitor IDENTIFIED BY 'ocean_monitor_pwd';

上面創建的用戶能夠在任何地方登錄。若是要限制在固定地址登錄,好比 localhost 登錄:mysql

CREATE USER ocean_monitor@localhost IDENTIFIED BY 'ocean_monitor_pwd';

建立數據庫linux

# 使用utf8編碼,不然中文會有問題
CREATE DATABASE ocean_monitor character set utf8;

受權 ocean_monitor 用戶擁有 ocean_monitor 數據庫的全部權限nginx

grant all on ocean_monitor.* to ocean_monitor identified by 'ocean_monitor_pwd';

若是是限制在 localhost 登陸的,則使用web

grant all on ocean_monitor.* to ocean_monitor@localhost identified by 'ocean_monitor_pwd';

使用 PIP 安裝依賴

爲mysql-python安裝依賴,這樣才能編譯安裝
若是是Ubuntu用戶sql

sudo apt-get install build-essential python-dev libmysqlclient-dev python-mysqldb

若是是Centos用戶

yum install gcc python-devel

安裝全部的依賴

# requirements.txt 是 django 項目目錄下,填寫的依賴包信息
pip install -r requirements.txt

測試 uWSGU

建立一個測試文件

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return ["Hello World"] # python2
    #return [b"Hello World"] # python3

而後,Run uWSGI:

uwsgi --http :8000 --wsgi-file test.py

若是出現錯誤,!!! no internal routing support, rebuild with pcre support !!!

sudo apt-get install libpcre3 libpcre3-dev
sudo pip uninstall uwsgi
sudo apt-get remove uwsgi
sudo pip install uwsgi

打開下面url,瀏覽器上應該顯示hello world

http://example.com:8000

若是顯示正確,說明下面3個環節是通暢的:

the web client <-> uWSGI <-> Python

測試Django

我看別人用的是 --module mysite.wsgi,可是建 Django項目時,並無生成這個文件,這裏十分奇怪。所以,使用 Django 生成的 wsgi.py

uwsgi --http :8000 --wsgi-file wsgi.py

若是顯示正確,說明下面3個環節是通暢的:

the web client <-> uWSGI <-> Django

Nginx 安裝與配置

Nginx 安裝

http://cantgis.blog.51cto.com/5788192/1540004

配置站點

vim /etc/nginx/conf.d/ocean_monitor.conf

添加配置信息

# ocean_monitor.conf

# the upstream component nginx needs to connect to
upstream django_ocean_monitor {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # for a web port socket (we'll use this first)
    server 127.0.0.1:8108;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8008;
    # the domain name it will serve for
    # substitute your machine's IP address or FQDN
    # Django 的 settings.py 文件中的 ALLOWED_HOSTS 要加上這裏設置的 server_name
    server_name localhost;
    charset     utf-8;

    gzip on;
    gzip_min_length 1000;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_vary on;
    # 禁用對 IE 6 使用 gzip 壓縮
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/xml+rss application/json;

    ## Individual nginx logs
    access_log  /var/log/nginx/ocean_monitor_access.log;
    error_log   /var/log/nginx/ocean_monitor_error.log;

    # max upload size
    client_max_body_size 8M;   # adjust to taste

    # Django media
    location /media  {
        # your Django project's media files - amend as required
        alias /home/python/ocean_monitor/media;  
    }

    location /static {
        # your Django project's static files - amend as required
        alias /home/python/ocean_monitor/static; 
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django_ocean_monitor;
        # the uwsgi_params file you installed
        #  增長 nginx 配置, uwsgi_params 文件在 /etc/nginx/ 目錄下
        include     /etc/nginx/uwsgi_params; 
    }
}

測試 nginx 的配置文件的語法是否正確

sudo nginx -t

重啓 nginx

service nginx restart

Django 配置

settings.py 配置

設置 ALLOWED_HOSTS

Django gives Bad Request (400) when DEBUG = False
The ALLOWED_HOSTS list should contain fully qualified host names, not urls. Leave of the port and the protocol. If you are using 127.0.0.1, I'd add localhost to the list too

ALLOWED_HOSTS = [
    # 加上本機的IP地址
    '192.168.137.146',
    '127.0.0.1', 
    'localhost'
]

You could also use * to match any host:

ALLOWED_HOSTS = ['*']

設置 DATABASES

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'ocean_monitor',
        'USER': 'ocean_monitor',
        'PASSWORD': 'ocean_monitor_pwd',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

初始化數據庫

python manage.py syncdb

啓動一下,測試 Django 有沒有問題

python manage.py runserver 0.0.0.0:8080

uWSGI 配置文件

在 Django 項目的根目錄添加 uwsgi.ini 文件

# uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/python/ocean_monitor
# Django's wsgi file
wsgi-file       = /home/python/ocean_monitor/ocean_monitor/wsgi.py
# module       = index.wsgi:application
# the virtualenv (full path)
# home            = /path/to/virtualenv
daemonize   = /home/python/ocean_monitor/ocean_monitor.log
# process-related settings
# master
master          = true
pidfile     = /tmp/ocean_monitor_master.pid
# maximum number of worker processes
processes       = 3
# the socket (use the full path to be safe
# socket          = /home/python/ocean_monitor/ocean_monitor.sock
socket          = 127.0.0.1:8108
# ... with appropriate permissions - may be needed
chmod-socket    = 664
# clear environment on exit
vacuum          = true

uwsgi 啓動與結束

# 啓動
uwsgi --ini uwsgi.ini
# 重啓
uwsgi --reload /tmp/ocean_monitor_master.pid
# 結束
uwsgi --stop /tmp/ocean_monitor_master.pid

若是出現錯誤 signal_pidfile()/kill(): No such process [core/uwsgi.c line 1627],是因爲 ocean_monitor_master.pid 的進程ID不正確,修改/tmp/ocean_monitor_master.pid爲正確的pid就能夠。

使用以下命令,查詢指定端口的進程id

sudo netstat -ap | grep 8108

修改 /tmp/ocean_monitor_master.pid 的進程id值

vim /tmp/ocean_monitor_master.pid

使用 supervisor 啓動 celery-worker 及 celery-beat

安裝 supervisor

安裝

pip install supervisor

安裝方法請參考

http://www.iitshare.com/supervisord-manage-process.html

生成配置文件

安裝好supervisor以後,默認是沒有生成配置文件的。能夠經過如下命令生成配置文件

echo_supervisord_conf > /etc/supervisord.conf

啓動

supervisord -c /etc/supervisord.conf  # 到指定路徑下去找配置文件

若是出現
another program is already listening on a port that one of our HTTP servers is configured to use

sudo unlink /etc/supervisor.sock

而後再次運行

supervisord -c /etc/supervisord.conf

supervisor 管理

supervisor 安裝完成後有兩個可用的命令行 supervisor 和 supervisorctl,命令使用解釋以下:

supervisord,初始啓動Supervisord,啓動、管理配置中設置的進程。
supervisorctl stop programxxx,中止某一個進程(programxxx),programxxx爲[program:chatdemon]裏配置的值,這個示例就是chatdemon。
supervisorctl start programxxx,啓動某個進程
supervisorctl restart programxxx,重啓某個進程
supervisorctl stop groupworker: ,重啓全部屬於名爲groupworker這個分組的進程(start,restart同理)
supervisorctl stop all,中止所有進程,注:start、restart、stop都不會載入最新的配置文件。
supervisorctl reload,載入最新的配置文件,中止原有進程並按新的配置啓動、管理全部進程。
supervisorctl update,根據最新的配置文件,啓動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啓。

注意:顯示用stop中止掉的進程,用reload或者update都不會自動重啓。

設置配置文件

vim /etc/supervisord.conf

在文件最後添加以下信息

[program:ocean_monitor_celery_worker]
process_name = ocean_monitor_celery_worker
command=python /home/python/ocean_monitor/manage.py celery worker --loglevel=info
directory=/home/python/ocean_monitor
autorestart=true
redirect_stderr=true
stdout_logfile = /var/log/supervisord/ocean_monitor_celery_worker.log
loglevel=info
[program:ocean_monitor_celery_beat]
process_name = ocean_monitor_celery_beat
command=python /home/python/ocean_monitor/manage.py celery beat --loglevel=info
directory=/home/python/ocean_monitor
autorestart=true
redirect_stderr=true
stdout_logfile = /var/log/supervisord/ocean_monitor_celery_beat.log
loglevel=info

用 supervisor 運行前,能夠先在命令行下啓動測試一下

python manage.py celery beat --loglevel=info
python manage.py celery worker --loglevel=info

啓動

# 根據最新的配置文件,啓動新配置或有改動的進程
# 配置沒有改動的進程不會受影響而重啓
supervisorctl update

防火牆設置及 SELinux 問題

設置防火牆開放端口(iptables)

/sbin/iptables -I INPUT -p tcp --dport 8008 -j ACCEPT

nginx permission denied 問題

由於忘記了 selinux 的問題,致使 trouble shooting 耽誤了很多時間。
因爲SELinux的問題,會致使nginx nginx permission denied 的問題,須要將 selinux 關閉。

查看SELinux狀態:

# 若是SELinux status參數爲enabled即爲開啓狀態
/usr/sbin/sestatus -v

也能夠用 getenforce 這個命令檢查

關閉SELinux

# 只是臨時關閉(不用重啓機器),重啓後問題仍然出現
# 設置SELinux 成爲permissive模式
setenforce 0 
# setenforce 1 # 設置SELinux 成爲enforcing模式

永久關閉,須要經過修改配置文件,可是須要重啓機器:

vim /etc/selinux/config

將SELINUX=enforcing 改成 SELINUX=disabled,重啓機器便可

shutdown -r now
相關文章
相關標籤/搜索