Centos 7搭建nginx+uwsgi+Django+mysql环境
本文仅为个人学习笔记,并非操作指引!若有问题,欢迎指正。
准备工作!
安装好系统自然不用说了,但我们在正式搭建环境之前,请先关闭防火墙,并修改selinux文件,确保您的环境能链接外网
# systemctl stop firewalld //关闭防火墙(临时)
#systemctl disable firewalld //取消防火墙开机启动
查看防火墙状态
# systemctl status firewalld
# vi /etc/selinux/config
将SELINUX=enforcing
改为SELINUX=disabled
:wq保存退出
正式开始!
安装python3.7.4
我安装的是python3.7.4版本,其他版本没有研究过,
- 下载安装包 ,源码包下载地址:https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz;
# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz //下载安装包命令(wget请自行安装)
- 安装依赖
编译安装gcc是必须的,libffi-devel gcc openssl是python3安装依赖包,事先安装好
#yum install libffi-devel gcc openssl -y //安装gcc、libffi-devel、 (必须安装)
- 正式安装
# tar xvf Python-3.7.4.tgz //解压事先下载好的源码压缩包Python-3.7.4.tgz,得到Python-3.7.4
# cd Python-3.7.4 //进入Python-3.7.4文件夹
# ls //查看当前目录下的文件
aclocal.m4 CODE_OF_CONDUCT.rst config.log config.sub configure.ac Grammar install-sh libpython3.7m.a m4 Makefile Makefile.pre.in Modules Parser PCbuild Programs pyconfig.h python python-config python-gdb.py setup.py
build config.guess config.status configure Doc Include Lib LICENSE Mac Makefile.pre Misc Objects PC profile-run-stamp pybuilddir.txt pyconfig.h.in Python python-config.py README.rst Tools
#./configure --prefix=/usr/local/python374 --enable-optimizations //编译,--prefix=/usr/local/python374指点安装带哪
#make && make install //编译成功后运行make && make install,这个时间有点长
注意./configure --prefix=/usr/local/python374 这里的位置下面我们会多次用到,如果不设置,默认安装位置是/usr/local/下,相关的执行文件都在/usr/local/bin下,到这里这文件夹下找即可
4. 创建软连接
安装完成后,在/usr/local下回有python374文件夹(这里是上一步编译安装时的路径,看字的路径就好)
[root@localhost bin]# cd /usr/local/python374/bin //进入到安装目录下的bin目录里
[root@localhost bin]# ls
2to3 2to3-3.7 django-admin django-admin.py easy_install-3.7 idle3 idle3.7 pip3 pip3.7 __pycache__ pydoc3 pydoc3.7 python3 python3.7 python3.7-config python3.7m python3.7m-config python3-config pyvenv pyvenv-3.7 uwsgi virtualenv
[root@localhost bin]# ln -s /usr/local/python374/bin/python3.7 /usr/bin/python3 //为了能让计算机找到python3 命令(和配置环境变量的意义一致)
[root@localhost bin]# ln -s /usr/local/python374/bin/pip3 /usr/bin/pip3 //同上
[root@localhost bin]# python3 //运行 python3,能看到如下信息,即为成功了
Python 3.7.4 (default, Aug 16 2019, 04:45:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
安装Django,并创建project
在做其他的安装之前,我们先安装一下Django,因为下面的uwsgi配置文件中将要用到路径
安装虚拟环境管理模块pip3 install virtualenv
pip3 install virtualenv
为virtualenv创建软连接
cd /usr/local/python374/bin
ln -s $(pwd)/virtualenv /usr/bin/virtualenv
创建虚拟环境
virtualenv +虚拟环境名称,我的虚拟环境在home目录下,名为virtualHome
virtualenv /home/virtualHome
安装Django (2.1版本)
进入虚拟环境
# cd /home/virtualHome/bin/
安装
网速快的话两三分钟就安装完了
# pip3 install django==2.1
安装完成后,直接运行django-admin可以查django相关的命令,此次不做详细介绍,详情可以查看官网
[root@localhost bin]# django-admin
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
创建项目
django-admin startproject 项目名 包名
# mkdir projectPackag //创建一个包
# django-admin startproject myproject ./projectPackage/ //创建项目
项目路径!!这里请仔细看一下,因为接下来要安装配置的uwsgi将要用到
/home/virtualHome/bin/projectPackage/myproject
项目中的settings.py
这部分内容比较多,才开始学,了解的不多,所以只写了几个关键的地方
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
#
#debug模式下,填写可以访问的ip这里我们配置如下
ALLOWED_HOSTS = ['*', ]
#配置mysql链接
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
#注释sqlite3这不分
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
#添加mysql的配置信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '数据库名称',
'USER': '用户名',
'PASSWORD': '密码',
'HOST': '数据库主机IP',
'PORT': '3306',
}
}
#静态资源
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
#放在哪你就写哪的位置这里这里在nginx中要配上,否则模板中加载不到
STATIC_ROOT = '/home/virtualHome/bin/projectPackage/static'
安装 mysqlclient,当然也可以通过其他方式比如pymysql模块,感兴趣的可以自己去问问度娘
#yum install mysql-devel //再次提醒,安装mysqlclient前,请安装mysql-devel,否则会报错
pip3 install mysqlclient
测试项目是否正常
# cd /home/virtualHome/bin/projectPackage
# python3 manage.py runserver 0.0.0.0:8000 & //注意,进入到项目包里,启动服务
#通过浏览器访问http://服务器ip:8000若能看到绿色小火箭,那说明成功了
安装uwsgi
使用pip3安装uwsgi模块,安装完成后,模块在刚才安装的python bin目录下
pip3 insyall uwsgi //卸载命令为 pip3 uninstall +模块名称
创建软连接
[root@localhost bin]# cd /usr/local/python374/bin //进入到安装目录下的bin目录里
[root@localhost bin]# ln -s /usr/local/python374/bin/uwsgi /usr/bin/uwsgi
编写配置文件
在/etc目录下创建一个uwsgi9090.ini文件,配置文件内容如下
[uwsgi]
socket = 127.0.0.1:9090 #这里的端口,在nginx配置时要用到
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid
daemonize = /var/log/uwsgi9090.log
chdir = /home/virtualHome/bin/projectPackage/ #此处为创建项目时的路径,到包即可
wsgi-file = myproject/wsgi.py #项目中wsgi
processes = 4
threads = 2
启动、关闭uwsgi
# uwsgi --ini /etc/uwsgi9090.ini & //启动
# ps aux | grep uwsig | awk '{print $2}' | xargs kill -9 //杀死uwsgi进程
检查是否启动
能看到uwsgi进程,9090端口已经监听(端口就是配置文件中socket指定的端口)
[root@localhost log]# ps aux | grep uwsgi
root 1463 0.0 0.7 324296 30172 ? Sl 05:36 0:00 uwsgi --ini /etc/uwsgi9090.ini
root 1464 0.0 0.6 324040 26068 ? Sl 05:36 0:00 uwsgi --ini /etc/uwsgi9090.ini
root 1465 0.0 0.6 324040 26200 ? Sl 05:36 0:00 uwsgi --ini /etc/uwsgi9090.ini
root 1466 0.0 0.6 324316 26700 ? Sl 05:36 0:00 uwsgi --ini /etc/uwsgi9090.ini
root 2618 0.0 0.0 112708 988 pts/0 R+ 11:17 0:00 grep --color=auto uwsgi
或者
[root@localhost log]# ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 127.0.0.1:9090 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
安装mysql
此处以5.7为例
这里不详细写了,可以参考一下:https://www.cnblogs.com/ianduin/p/7679239.html
注意,这里请将mysql-devel也一起安装一下,否则后面安装mylsqclient会报错
安装nginx
参考https://www.cnblogs.com/songxingzhu/p/8568432.html
- 添加源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装Nginx
sudo yum install -y nginx
- 启动Nginx并设置开机自动运行
sudo systemctl start nginx.service
sudo systemctl enable nginx.service
- 启动Nginx
systemctl start nginx //重启restart、关闭stop;默认端口为80,可以使用ss -lnt命令查看是否监听
配置nginx与uwsgi
参考:https://www.cnblogs.com/freely/p/8027937.html
进入到nginx配置目录下
/etc/nginx/conf.d
复制一份default.conf,
# cp default.conf uwsgi.conf
# mv default.conf default.conf_bak
配置文件如下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
#一定要与uwsgi中配置的一直!!!!!!!!!
}
# 静态文件路径
#我的静态资源就放在项目包里,与项目平级,这里根据自己的实际情况修改
location /static {
alias /home/virtualHome/bin/projectPackage/static/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
重启nginx
systemctl restart nginx