参考文档https://www.jianshu.com/p/cafe0bf74fad
参考文档https://blog.csdn.net/rudy5348/article/details/79299162
centos7+python3.6+nginx1.12.2+django+uwsgi+postgresql搭建pythonweb
注意:需要先关闭selinux
安装python3.6
1、安装python需要的依赖
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ zilb zlib-devel postgresql-devel*
2、备份centos7自带的python
[root@localhost bin]# cd /usr/bin/
[root@localhost bin]# mv python python.bak
3、创建python安装的目录,rz上传我本地的python安装包,也可在官网下载https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
[root@localhost usr]# mkdir -p /usr/local/python/python3
[root@localhost usr]# cd /usr/local/python/
[root@localhost python]# rz
4、解压下载好的安装包
[root@localhost python]#tar -zxvf Python-3.6.8.tgz
5、进入解压后的路径,编译到指定路径
[root@localhost Python-3.6.8]# pwd
/usr/local/python/Python-3.6.8
[root@localhost Python-3.6.8]# ./configure --prefix=/usr/local/python/python3
6、安装
[root@localhost Python-3.6.8]# make && make install
7、创建软连接,并检查python版本
[root@localhost bin]# cd /usr/bin/
[root@localhost bin]# ln -s /usr/local/python/python3/bin/python3 /usr/bin/python
[root@localhost bin]# python -V
Python 3.6.8
8、给pip创建软连接,并更新
[root@localhost bin]# cd /usr/bin/
[root@localhost bin]# ln -s /usr/local/python/python3/bin/pip3 /usr/bin/pip
[root@localhost bin]# pip install --upgrade pip
9、因为centos7的yum需要用到Python2.7,需要重新指定yum
[root@localhost bin]# vim /usr/bin/yum
将#!/usr/bin/python改成#!/usr/bin/python2.7
[root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python改成#!/usr/bin/python2.7
安装django和uwsgi
1、新建requirements.txt,写入如下内容,并安装django和一些项目需要的依赖包
[root@localhost python3]# vim requirements.txt
[root@localhost python3]# cat requirements.txt
django==1.11
psycopg2
psycopg2-binary
requests==2.21.0
uwsgi
[root@localhost python3]# pip install -r requirements.txt
2、修改pip的安装源(因为pip自带的安装源访问太慢)
修改 ~/.pip/pip.conf (没有就创建一个), 写入如下内容
[global]
timeout = 6000
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
3、pip list #查看已安装的依赖包
[root@localhost python3]# pip list
Package Version
--------------- --------
certifi 2019.3.9
chardet 3.0.4
Django 1.11
idna 2.8
pip 19.2.3
psycopg2 2.7.7
psycopg2-binary 2.8.2
pytz 2019.1
requests 2.21.0
setuptools 40.6.2
sqlparse 0.3.0
urllib3 1.24.3
uWSGI 2.0.18
wheel 0.33.4
安装nginx
1、安装epel源
[root@localhost ~]# yum -y install epel-release
2、安装nginx
[root@localhost ~]# yum -y install nginx
3、开机自启
[root@localhost /]# systemctl start nginx
[root@localhost /]# systemctl enable nginx
安装postgresql
1、下载rpm包
wget https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
2、解压
[root@localhost ~]# rpm -ivh pgdg-centos10-10-2.noarch.rpm
3、安装
[root@localhost ~]# yum -y install postgresql10-contrib postgresql10-server
4、初始化数据库,ok表示成功
[root@localhost ~]# /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK
5、启动postgresql并设置开机自启
[root@localhost ~]# systemctl start postgresql-10 .service
[root@localhost ~]# systemctl enable postgresql-10.service
6、登录,切换到postgres用户(postgresql在安装时默认添加用户postgres),运行psql进入数据库
[root@localhost pgsql]# su - postgres
上一次登录:一 8月 26 13:10:26 CST 2019pts/0 上
-bash-4.2$ psql
psql (10.10)
输入 "help" 来获取帮助信息.
postgres=#
7、给postgres用户,设置密码
postgres=# ALTER USER postgres WITH PASSWORD '密码';
ALTER ROLE
8、退出数据库
postgres=# :\q
9、默认情况下postgresql是不用密码不支持远程登录的。我们需要修改配置文件
[root@localhost ~]# vim /var/lib/pgsql/10/data/pg_hba.conf
#修改后内容如下
10、修改远程访问,允许所有人,端口为3433
[root@localhost ~]# vim /var/lib/pgsql/10/data/postgresql.conf
#修改后内容如下
11、修改完成后,重启数据库
[root@localhost ~]# systemctl restart postgresql-10.service
uwsgi配置
[root@localhost ~]# vim /data1/captcha/captcha/uwsgi.ini
新建uwsgi.ini,写入如下内容
[uwsgi]
chdir = /data1/www/captcha.dachagui.com/captcha
socket = 127.0.0.1:8000
module = captcha.wsgi:application
processes = 2
master = 1
buffer-size = 88192
daemonize = /data1/log/uwsgi_log/uwsgi.log
pidfile = /data1/www/captcha.dachagui.com/captcha/uwsgi.pid
配置nginx
[root@localhost ~]# vim /etc/nginx
修改后内容如下
[root@localhost ~]# mkdir -p /etc/nginx/vhost
[root@localhost ~]# vim /etc/nginx/vhost/jango_test.conf
#写入内容如下
server{
listen 80; #监听80端口
server_name 127.0.0.1;
charset utf-8;
access_log /var/log/nginx/access.log; #日志文件位置
error_log /var/log/nginx/error.log;
client_max_body_size 75M;
location / { #访问/时
include uwsgi_params; #加载uwsgi模块
uwsgi_pass 127.0.0.1:8000; #将连接转到该IP
uwsgi_read_timeout 20;
}
}
保存后重启nginx
[root@localhost ~]# systemctl restart nginx
uwsgi启动django
[root@localhost captcha]# pwd
/data1/www/captcha.dachagui.com/captcha
[root@localhost captcha]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
成功访问到项目