參考文檔http://www.reibang.com/p/cafe0bf74fad
參考文檔https://blog.csdn.net/rudy5348/article/details/79299162
centos7+python3.6+nginx1.12.2+django+uwsgi+postgresql搭建pythonweb
注意:需要先關(guān)閉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、創(chuàng)建python安裝的目錄漓踢,rz上傳我本地的python安裝包,也可在官網(wǎng)下載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喧半、進(jìn)入解壓后的路徑,編譯到指定路徑
[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挺据、創(chuàng)建軟連接搁拙,并檢查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創(chuàng)建軟連接夺颤,并更新
[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忙迁,寫入如下內(nèi)容贤牛,并安裝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?(沒有就創(chuàng)建一個)王暗, 寫入如下內(nèi)容
[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隐绵、初始化數(shù)據(jù)庫之众,ok表示成功
[root@localhost ~]# /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK
5、啟動postgresql并設(shè)置開機自啟
[root@localhost ~]# systemctl start postgresql-10 .service
[root@localhost ~]# systemctl enable postgresql-10.service
6依许、登錄棺禾,切換到postgres用戶(postgresql在安裝時默認(rèn)添加用戶postgres),運行psql進(jìn)入數(shù)據(jù)庫
[root@localhost pgsql]# su - postgres
上一次登錄:一 8月 26 13:10:26 CST 2019pts/0 上
-bash-4.2$ psql
psql (10.10)
輸入 "help" 來獲取幫助信息.
postgres=#
7峭跳、給postgres用戶膘婶,設(shè)置密碼
postgres=# ALTER USER postgres WITH PASSWORD '密碼';
ALTER ROLE
8、退出數(shù)據(jù)庫
postgres=# :\q
9蛀醉、默認(rèn)情況下postgresql是不用密碼不支持遠(yuǎn)程登錄的悬襟。我們需要修改配置文件
[root@localhost ~]#?vim /var/lib/pgsql/10/data/pg_hba.conf
#修改后內(nèi)容如下
10、修改遠(yuǎn)程訪問拯刁,允許所有人脊岳,端口為3433
[root@localhost ~]#?vim /var/lib/pgsql/10/data/postgresql.conf
#修改后內(nèi)容如下
11、修改完成后垛玻,重啟數(shù)據(jù)庫
[root@localhost ~]# systemctl restart?postgresql-10.service
uwsgi配置
[root@localhost ~]#?vim /data1/captcha/captcha/uwsgi.ini
新建uwsgi.ini割捅,寫入如下內(nèi)容
[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
修改后內(nèi)容如下
[root@localhost ~]# mkdir -p /etc/nginx/vhost
[root@localhost ~]# vim?/etc/nginx/vhost/jango_test.conf
#寫入內(nèi)容如下
server{
? ? ? ? listen 80;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #監(jiān)聽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;? ? ? ? #將連接轉(zhuǎn)到該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
成功訪問到項目