龚哥哥 - 山里男儿 爱生活、做自己!
魔鬼部署系统搭建 Nginx+Uwsgi+Django
发表于 2017-8-23 | 浏览(44396) | 开源项目

效果图

Image

流程图

Image

项目地址

https://github.com/gongfuxiang/mogui

https://coding.net/u/gongfuxiang/p/mogui/git


其它相关可参考本博客中的其它文章

1、nginx
2、git ssh部署
3、mysql

基础信息

系统         CentOS7(7.3.1611)
Python      2.7.5
Django      1.11.3
Vue         2.4.0
Element     1.4.2

centos6自带python 2.4.3, 我们也可以升级到python2.7.13(这一步可跳过)

https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13
./configure --prefix=/usr/local
make && make altinstall

安装setuptools

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar -zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install

安装pip

wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz
tar -zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install

安装python-devel

yum -y install python-devel
yum -y install sqlite-devel

pip安装uwsgi

pip install uwsgi

测试uwsgi是否正常运行 创建 test.py 文件

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

uwsgi --http :8008 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8008,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。

pip安装django

pip install django==1.11.3

测试 django 是否正常,运行

django-admin.py startproject hello
cd hello
python manage.py runserver 0.0.0.0:8008
在浏览器内输入:http://127.0.0.1:8008,检查django是否运行正常。

安装项目需要用到的python模块

yum -y install mysql-devel
pip install mysql-python 或 pip install mysqlclient

开始部署魔鬼部署系统(github已部署好ssh)

mkdir -p /data/www
cd /data/www
git clone https://github.com/gongfuxiang/mogui.git

修改数据库配置文件

/data/www/mogui/mogui/common/config.py
修改以下配置信息保存即可
# 数据库
db = {
    'name' : 'mogui',       # 数据库名称
    'user' : 'root',        # 用户名
    'pwd'  : 'root',        # 密码
    'host' : 'localhost',   # 连接地址
    'port' : 3306           # 端口号
}

还记得新增实际部署的域名或ip

/data/www/mogui/mogui/settings.py

修改 ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

创建数据表

cd /data/www/mogui
python manage.py migrate

创建uwsgi配置文件 创建 /etc/uwsgi.ini

详细可参考官网文档 https://uwsgi-docs.readthedocs.io/en/latest/

[uwsgi]
chdir           = /data/www/mogui
module          = mogui.wsgi
master          = true
processes       = 10
socket          = 127.0.0.1:9090
vacuum          = true
pidfile         = /var/run/uwsgi.pid    
daemonize       = /var/log/uwsgi.log
uid             = 0

uwsgi常用操作

启动uwsgi
    uwsgi --ini /etc/uwsgi.ini

uwsgi重启
    kill -HUP `cat /var/run/uwsgi.pid`

uwsgi停止
    killall -9 uwsgi

nginx配置(记得重启)

upstream django {
    server 127.0.0.1:9090;
}

server {
    listen      80 default;
    server_name _;
    charset     utf-8;
    client_max_body_size 75M;
    uwsgi_read_timeout 1800;
    uwsgi_send_timeout 300;
    proxy_read_timeout 300;

    location /public {
        alias /data/www/mogui/public;
    }

    location / {
        uwsgi_pass  django;
        include     uwsgi_params;
    }
}

访问 http://127.0.0.7

阅读全文

Python中文转拼音
发表于 2016-11-2 | 浏览(13187) | Python

最近写一个项目的时候,需要将汉字转化成拼音,在github上找到一个现成的库。

Python汉字转拼音 GitHub地址

1、使用如下

from pinyin import PinYin

test = PinYin()
test.load_word()
print test.hanzi2pinyin(string='钓鱼岛是中国的')
print test.hanzi2pinyin_split(string='钓鱼岛是中国的', split="-")

输出
['diao', 'yu', 'dao', 'shi', 'zhong', 'guo', 'de']
'diao-yu-dao-shi-zhong-guo-de'

2、在中文与英文混合的时候英文会丢失,所以我们需要对库进行一点调整。

2.1、hanzi2pinyin方法调整

# 原始
    def hanzi2pinyin(self, string=""):
        result = []
        if not isinstance(string, unicode):
            string = string.decode("utf-8")

        for char in string:
            key = '%X' % ord(char)
            result.append(self.word_dict.get(key, char).split()[0][:-1].lower())

        return result

# 调整后
    def hanzi2pinyin(self, string=""):
        result = []
        if not isinstance(string, unicode):
            string = string.decode("utf-8")
        en = ''
        for char in string:
            key = '%X' % ord(char)
            if not self.word_dict.get(key) :
                # 为字母或数字才进入(排除特殊字符)
                if(char.isalpha() == True or char.isdigit() == True) :
                    en += char
                else :
                    # 空格则分割数据
                    if(char.isspace() == True) :
                        result.append(en)
                        en = ''
            else:
                # en有数据则先处理
                if(len(en) > 0) :
                    result.append(en)
                    en = ''
                # 中文转拼音
                result.append(self.word_dict.get(key, char).split()[0][:-1].lower())
        # 防止全是英文或数字
        if(len(en) > 0) :
            result.append(en)
            en = ''

        return result

2.2、hanzi2pinyin_split方法调整

# 原始
    def hanzi2pinyin_split(self, string="", split=""):
        result = self.hanzi2pinyin(string=string)
        if split == "":
            return result
        else:
            return split.join(result)

# 调整后
    def hanzi2pinyin_split(self, string="", split=""):
        return split.join(self.hanzi2pinyin(string=string))

3、调整后的效果

print test.hanzi2pinyin(string='钓鱼岛是hello中国的')
print test.hanzi2pinyin_split(string='钓鱼岛是hello中国world的', split="-")

输出
['diao', 'yu', 'dao', 'shi', 'hello', 'zhong', 'guo', 'de']
diao-yu-dao-shi-hello-zhong-guo-world-de

阅读全文

TOP