在云服务器上从零搭建nginx+uwsgi+django服务器

笔者使用腾讯云服务器,centos7 64位镜像,自带 python 2.7.5 无需安装
现在安装 pip

    wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz
    tar zvxf 1.5.5.tar.gz
    cd pip-1.5.5/
    python setup.py install

可能出现错误:
ImportError: No module named setuptools
需要安装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 install

安装mysql:(参考http://www.centoscn.com/mysql/2016/0315/6844.html

    yum install libaio # 安装依赖包,这里已经安装
    yum install mysql-community-server

配置mysql:

    whereis mysql  # 可以查看安装目录
    systemctl start  mysqld  # 启动服务
    systemctl status  mysqld # 查看状态
    mysql_secure_installation # 安全设置

字符集设置:
修改 /etc/my.cnf 文件,添加字符集的设置

[mysqld]   
character_set_server = utf8

[mysql]
default-character-set = utf8
重启 MySQL
systemctl restart mysqld

安装git:

yum install git

安装nginx:

yum install nginx

pip安装uwsgi:

pip install uwsgi
出现错误:you need a C compiler to build uWSGI

yum install -y gcc gcc-c++  

错误:fatal error: Python.h

yum install python-devel

安装django依赖库:

sudo yum install python-devel
sudo yum install zlib-devel
sudo yum install libjpeg-turbo-devel
pip install pillow

使用uwsgi运行测试文件:
uwsgi –http :8001 –wsgi-file test.py
test.py

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

使用git导入项目:

git init
git config -global user.email "..."
git config --global user.name "..."
git remote add origin https://github.com/...
git fetch origin linux
git commit –m “init” # 先commit一次创建本地分支
git merge origin/linux

配置nginx:
whereis nginx # 查看nginx安装目录
编辑配置文件nginx.conf:

server {
        listen       8080;
        server_name  www.yours.cn;
        root         /root/SHBproject;
        index        root.template.login.html
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        access_log  /var/log/nginx/mindg.access.log;

        location / {
        include    uwsgi_params;
        uwsgi_pass 127.0.0.1:9001;
        }

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

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

启动uwsgi:

    uwsgi --socket 127.0.0.1:9001 --chdir /root/SHBproject/ --wsgi-file /root/SHBproject/SecondHandsBook/wsgi.py -d uwsgi.log # 运行uwsgi

问题: Internal Server Error
修改wsgi.py:

import os
import sys
    from django.core.wsgi import get_wsgi_application
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
    application = get_wsgi_application()

测试 uwsgi –http :8001 –wsgi-file SecondHandsBook/wsgi.py
发现是django数据库配置问题,不加赘述。

注意:

    yum install -y mysql-devel python-devel python-setuptools
    pip install MySQL-python 

错误:
probably another instance of uWSGI is running on the same address
查看端口调用:

    lsof -i :9001
    kill -9 5632

重启:

uwsgi --socket 127.0.0.1:9001 --chdir /root/SHBproject/ --wsgi-file /root/SHBproject/SecondHandsBook/wsgi.py

使用ini启动uwsgi:
uwsig.ini

    [uwsgi]
    vhost = false
    socket = 127.0.0.1:9001
    master = true
    enable-threads = true
    daemonize = /var/log/uwsgi.log    #后台启动,并把日志记录到指定文件
    workers = 1
    wsgi-file = /root/SHBproject/SecondHandsBook/wsgi.py
chdir = /root/SHBproject

启动命令:uwsgi –ini /root/SHBproject/uwsgi.ini&

部署就到此为止了,未来的优化会持续更新。。。
更新git仓库:

    git branch qqcloud
    git checkout qqcloud
    git commit -m "server management" –a
    git push -u origin qqcloud
    git add uwsgi.ini
    git commit -m "add uwsgi.ini"

还有一些问题:
1. The _imagingft C module is not installed
这个是由于PIL没有编译freetype导致的,
https://my.oschina.net/u/993130/blog/214454
2. 权限问题导致Nginx 403 Forbidden错误的解决方法
在nginx.conf头部加入一行:user root;
3. 配置location的时候一定要注意绝对路径和相对路径的使用

    location /static/ {
                alias /root/SHBproject/static/;
            }

        location /media/ {
            alias /root/SHBproject/media/;
        }

        location / {            
            include    uwsgi_params;
            uwsgi_pass 127.0.0.1:9001;
        }

猜你喜欢

转载自blog.csdn.net/vic_torsun/article/details/70738600