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

1 篇文章 0 订阅
1 篇文章 0 订阅

笔者使用腾讯云服务器,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;
        }

最近阿里云有相关的推广活动,点击快速查看

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ubuntu是一个自由开源的操作系统,其中有很多的工具和服务可以用于搭建与开发Web应用。其中,常用的Web应用栈是使用Nginx作为Web服务器UWSGI作为应用程序服务器Django作为Web框架,MySQL作为关系型数据库。 要安装和配置这些服务,需要进行以下步骤: 1. 安装Ubuntu操作系统,可以选择最新版本的Ubuntu LTS或者最新的Ubuntu发行版。 2. 安装Nginx服务,并配置Nginx服务器来处理并转发HTTP请求。这里可以使用apt-get install命令来安装Nginx包。 3. 安装UWSGI服务,并配置UWSGI服务器来处理Django应用程序。这里可以使用pip install命令来安装UWSGI包。 4. 安装Django,并编写一个基本的Django应用程序。然后配置UWSGI服务器以运行Django应用程序。可以使用pip install命令来安装Django包。可以使用此教程 - https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html 5. 安装MySQL数据库服务,并配置MySQL服务器以使用Django应用程序。可以使用apt-get install命令来安装MySQL包。 6. 在Django应用程序中配置MySQL数据库连接参数,并创建 MySQL数据库及表。 7. 配置Nginx服务器以使用UWSGI服务器来处理Django应用程序。可以使用上面的教程来完成这一步骤。 8. 启动NginxUWSGI服务器,并测试Django应用程序是否可以访问。 在进行以上的步骤时,需要注意安全措施,防止 Web 应用程序被攻击和入侵。同时,也需要进行充分的测试,保证 Web 应用程序的稳定性和可用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丧心病狂の程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值