python web服務器環境搭建(安裝python3並與python2共存)


 

服務器為cnetos7,以下環境以此為基准。

1、安裝python3並與python2共存

1.1 python檢測

centos7默認安裝了 python2.7.5 因為一些命令要用它比如yum,它使用的是python2.7.5。

使用 python -V 命令查看一下是否安裝Python。

[root@localhost bin]# python -V Python 2.7.5 

然后使用命令 which python 查看一下Python可執行文件的位置。

[root@localhost bin]# which python /usr/bin/python [root@localhost bin]# cd /usr/bin/ [root@localhost bin]# ll py* lrwxrwxrwx. 1 root root 7 4月 8 2017 python -> python2 lrwxrwxrwx. 1 root root 9 4月 8 2017 python2 -> python2.7 -rwxr-xr-x. 1 root root 7136 6月 18 2014 python2.7 

python 指向的是python2.7。因為我們要安裝python3版本,所以python要指向python3才行。

先yum安裝必要的包,用於下載編譯python3。

[root@localhost bin]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make 

安裝后防止錯誤,先做備份:

[root@localhost bin]# mv python python.bak [root@localhost bin]# ll pytho* lrwxrwxrwx. 1 root root 9 4月 8 2017 python2 -> python2.7 -rwxr-xr-x. 1 root root 7136 6月 18 2014 python2.7 lrwxrwxrwx. 1 root root 7 4月 8 2017 python.bak -> python2 

1.2 安裝python3

獲取安裝包。

[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz 

解壓。

[root@localhost ~]# tar -xvJf Python-3.6.4.tar.xz 

切進目錄。

[root@localhost ~]# cd Python-3.6.4/ 

編譯,安裝。

[root@localhost Python-3.6.4]# ./configure prefix=/usr/local/python3 [root@localhost Python-3.6.4]# make && make install 

安裝完畢,/usr/local/目錄下就會有python3了。

[root@localhost Python-3.6.4]# cd /usr/local/python3/ [root@localhost python3]# ll 總用量 4 drwxr-xr-x 2 root root 4096 3月 21 18:37 bin drwxr-xr-x 3 root root 23 3月 21 18:37 include drwxr-xr-x 4 root root 60 3月 21 18:37 lib drwxr-xr-x 3 root root 16 3月 21 18:37 share 

然后我們可以添加軟鏈到執行目錄下/usr/bin。

[root@localhost Python-3.6.4]# ln -s /usr/local/python3/bin/python3 /usr/bin/python 

同時pip也建立一個軟連接。

[root@localhost bin]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip 

然后軟連接創建完成,查看當前python版本。

[root@localhost python3]# python -V Python 3.6.4 [root@localhost python3]# python2 -V Python 2.7.5 [root@localhost python3]# cd /usr/bin/ [root@localhost bin]# ll python* lrwxrwxrwx 1 root root 30 3月 21 18:40 python -> /usr/local/python3/bin/python3 lrwxrwxrwx. 1 root root 9 4月 8 2017 python2 -> python2.7 -rwxr-xr-x. 1 root root 7136 6月 18 2014 python2.7 

最后因為執行yum需要python2的版本,所以我們還要修改yum的配置。

[root@localhost bin]# vim /usr/bin/yum 把#!/usr/bin/python 修改為 #!/usr/bin/python2 

同理。

[root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down 

這樣python3版本就安裝完成,同時python2也存在。

2、uwigi安裝

這個很坑,老司機彎都轉不過來。

坑如下:

說是在基於Debian的發行版上要安裝依賴,依賴么,我懂的。

apt-get install build-essential python-dev

然后我去找centos版本的相關依賴。

然后下面這樣,python-dev的包在centos的yum中不叫python-dev,而是python-devel.

[root@localhost local]# yum install python-devel 

然后build-essential對應的工具說是下面這樣。

[root@localhost local]# yum groupinstall "Development Tools" 

然后centos警告錯誤啊。

[root@localhost local]# yum groupinstall "Development Tools" 已加載插件:fastestmirror, langpacks 沒有安裝組信息文件 Maybe run: yum groups mark convert (see man yum) Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.163.com * updates: mirrors.nju.edu.cn 警告:分組 development 不包含任何可安裝軟件包。 Maybe run: yum groups mark install (see man yum) 指定組中沒有可安裝或升級的軟件包 

大家都是如是說:警告:分組 development 不包含任何可安裝軟件包。

尼瑪,然后我就把這么說的全部包都安裝了,兩列,必要項和可選項。

[root@localhost local]# yum install ElectricFence ant babel bzr chrpath cmake compat-gcc-44 compat-gcc-44-c++ cvs dejagnu expect gcc-gnat gcc-objc gcc-objc++ imake javapackages-tools ksc libstdc++-docs mercurial mod_dav_svn nasm perltidy python-docs rpmdevtools rpmlint systemtap-sdt-devel systemtap-server [root@localhost local]# yum install autoconf automake binutils bison flex gcc gcc-c++ gettext libtool make patch pkgconfig redhat-rpm-config rpm-build rpm-sign 

安裝后裝uwsgi,用pip方式。

[root@localhost local]# pip install uwsgi 

fuck,裝完沒可執行命令啊。

[root@localhost local]# uwsgi bash: uwsgi: 未找到命令... 

然后下面一頓倒騰,甚至py3我都重裝了,車毀人亡。

最后不得已還是求助官檔吧。

獲取uwsgi

正確方法就是這句,使用網絡安裝器:

curl http://uwsgi.it/install | bash -s default /usr/local/uwsgi 

卧槽了,吃棗葯丸。繼續前進!

3、nginx安裝

以前直接用lnmp跑的站,然后有現成的,修修改改直接跑。

nignx配置文件:

server
{
    listen 80 default_server; server_name daocoder.ccom; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8001; } } 

4、集成

4.1 nginx+uwsgi配置

之前nginx配置文件已有。

server
{
    listen 80 default_server; server_name daocoder.ccom; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8001; } } 

下面uwsgi跑起來,編輯配置文件,那里創建都行,我是直接放在flask項目目錄下創建uwsgi.ini內容如下:

[uwsgi]
# 啟動程序時所使用的地址和端口,通常在本地運行flask項目,地址和端口是127.0.0.1:5000 # 不過在服務器上是通過uwsgi設置端口,通過uwsgi來啟動項目,也就是說啟動了uwsgi,也就啟動了項目 socket = 127.0.0.1:8001 # 項目目錄 # chdir = /home/wwwroot/flask/ # flask程序的啟動文件,通常在本地是通過運行 python manage.py runserver 來啟動項目的 wsgi-file = /home/wwwroot/flask/app.py # 程序內啟用的application變量名 callable = app # 處理器個數 processes = 2 # 線程個數 threads = 2 # 獲取uwsgi統計信息的服務地址 stats = 127.0.0.1:9191 

4.2 flask項目

先下載flask。

[root@localhost flask]# pip install flask 

我的路徑是:

[root@localhost flask]# pwd /home/wwwroot/flask 

此文件加創建文件如下:

flask
    - templates/
        - index.html
    - app.py
    - uwsgi.ini

app.py文件內容:

#! /usr/bin/python # -*- coding:utf-8 -*- from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run() 

templates/index.html的內容:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>flask</title> </head> <body> <div>hello daocoder</div> </body> </html> 

uwsgi.ini的內容在前面已有。

完了后,nginx重新啟動,uwsgi跑起來。

[root@localhost flask]# service nginx restart [root@localhost flask]# uwsgi uwsgi.ini 

最后瀏覽器訪問你的服務器,bingo。

再一個問題在於uwsgi於前台進行,無法后台跑,有說emperor參數,我沒嘗試,下面會使用supervisor來改變這方式。

5 安裝配置supervisor

5.1 安裝supervisor

嘗試直接安裝,成功后可直接到配置下一節。

[root@localhost flask]# pip install supervisor 

下面是我遇到的坑。

第一次失敗。

[root@localhost flask]# pip install supervisor Collecting supervisor Downloading supervisor-3.3.4.tar.gz (419kB) 100% |████████████████████████████████| 419kB 5.1kB/s Complete output from command python setup.py egg_info: Supervisor requires Python 2.4 or later but does not work on any version of Python 3. You are using version 3.6.2 (default, Mar 22 2018, 07:59:08) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]. Please install using a supported version. ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-1n5dvn1g/supervisor/ 

好像不支持python3,那就yum安裝。

[root@localhost flask]# yum install supervisor 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.163.com * updates: mirrors.nju.edu.cn 沒有可用軟件包 supervisor。 錯誤:無須任何處理 [root@localhost flask]# yum info supervisor 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * extras: mirrors.163.com * updates: mirrors.nju.edu.cn 錯誤:沒有匹配的軟件包可以列出 

無解,那就用python2指定pip安裝。

[root@localhost bin]# python2 -m pip install supervisor /usr/bin/python2: No module named pip 

無力吐槽,原系統用的好像還是easy_install把管理工具這個,表示從來沒用過。那就軟連接先切回python2的版本再利用這個工具安裝supervisor。

[root@localhost bin]# easy_install supervisor Searching for supervisor Reading https://pypi.python.org/simple/supervisor/ Best match: supervisor 3.3.4 Downloading https://pypi.python.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz#md5=f1814d71d820ddfa8c86d46a72314cec Processing supervisor-3.3.4.tar.gz Writing /tmp/easy_install-aL9rb7/supervisor-3.3.4/setup.cfg Running supervisor-3.3.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-aL9rb7/supervisor-3.3.4/egg-dist-tmp-_eRZYl warning: no previously-included files matching '*' found under directory 'docs/.build' Adding supervisor 3.3.4 to easy-install.pth file Installing echo_supervisord_conf script to /usr/bin Installing pidproxy script to /usr/bin Installing supervisorctl script to /usr/bin Installing supervisord script to /usr/bin Installed /usr/lib/python2.7/site-packages/supervisor-3.3.4-py2.7.egg Processing dependencies for supervisor Searching for meld3>=0.6.5 Reading https://pypi.python.org/simple/meld3/ Best match: meld3 1.0.2 Downloading https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91 Processing meld3-1.0.2.tar.gz Writing /tmp/easy_install-WgVnBv/meld3-1.0.2/setup.cfg Running meld3-1.0.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-WgVnBv/meld3-1.0.2/egg-dist-tmp-fZ0vyo zip_safe flag not set; analyzing archive contents... Adding meld3 1.0.2 to easy-install.pth file Installed /usr/lib/python2.7/site-packages/meld3-1.0.2-py2.7.egg Finished processing dependencies for supervisor 

安裝完了,然后當然是切回來python3的版本了,下面是py2切回py3的操作。

[root@localhost bin]# ll pytho* lrwxrwxrwx. 1 root root 7 4月 8 2017 python -> python2 lrwxrwxrwx 1 root root 9 3月 21 19:41 python2 -> python2.7 -rwxr-xr-x 1 root root 7136 8月 4 2017 python2.7 lrwxrwxrwx 1 root root 30 3月 22 08:04 python3.bak -> /usr/local/python3/bin/python3 [root@localhost bin]# mv python python2.bak [root@localhost bin]# ll pytho* lrwxrwxrwx 1 root root 9 3月 21 19:41 python2 -> python2.7 -rwxr-xr-x 1 root root 7136 8月 4 2017 python2.7 lrwxrwxrwx. 1 root root 7 4月 8 2017 python2.bak -> python2 lrwxrwxrwx 1 root root 30 3月 22 08:04 python3.bak -> /usr/local/python3/bin/python3 [root@localhost bin]# mv python3.bak python [root@localhost bin]# ll pytho* lrwxrwxrwx 1 root root 30 3月 22 08:04 python -> /usr/local/python3/bin/python3 lrwxrwxrwx 1 root root 9 3月 21 19:41 python2 -> python2.7 -rwxr-xr-x 1 root root 7136 8月 4 2017 python2.7 lrwxrwxrwx. 1 root root 7 4月 8 2017 python2.bak -> python2 

這里發現確實pip安裝后可能生成不了可執行命令,未找到相應答案去提了個問題pip安裝模塊后沒有生成相應的可執行命令。當然這是臆斷的,可能是我自己的服務器環境問題。以后估計還是要踩這個坑。

上面是自己煞筆了,pip安裝后直接放在pythons/bin目錄了,還要自己建立軟連接到/usr/bin下面,這個確實蛋疼。

5.2 配置supervisor

因為上面的包是安裝在py2里面的,所以不能直接跑。

編輯下/usr/bin/supervisord。

#!/usr/bin/python 改成 #!/usr/bin/python2 

同理修改這幾個命令文件:/usr/bin/echo_supervisord_conf、/usr/bin/supervisorctl。

官檔 supervisor

都改完了,先看寫幫助。

[root@localhost bin]# supervisord -h supervisord -- run a set of applications as daemons. Usage: /usr/bin/supervisord [options] Options: -c/--configuration FILENAME -- configuration file path (searches if not given) -n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file) -h/--help -- print this usage message and exit -v/--version -- print supervisord version number and exit -u/--user USER -- run supervisord as this user (or numeric uid) -m/--umask UMASK -- use this umask for daemon subprocess (default is 022) -d/--directory DIRECTORY -- directory to chdir to when daemonized -l/--logfile FILENAME -- use FILENAME as logfile path -y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile -z/--logfile_backups NUM -- number of backups to keep when max bytes reached -e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical) -j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME -i/--identifier STR -- identifier used for this instance of supervisord -q/--childlogdir DIRECTORY -- the log directory for child process logs -k/--nocleanup -- prevent the process from performing cleanup (removal of old automatic child log files) at startup. -a/--minfds NUM -- the minimum number of file descriptors for start success -t/--strip_ansi -- strip ansi escape codes from process output --minprocs NUM -- the minimum number of processes available for start success --profile_options OPTIONS -- run supervisord under profiler and output results based on OPTIONS, which is a comma-sep'd list of 'cumulative', 'calls', and/or 'callers', e.g. 'cumulative,callers') 

然后這里說明下supervisor這個工具會在運行時,跑兩個進程。一個是作為后台運行的服務supervisord,另一個是客戶端supervisorctl用來交互supervisord

至此大概說完,下面開始配置supervisord。

1、創建默認的配置文件。

[root@localhost bin]# echo_supervisord_config > /etc/supervisord.conf 

配置文件具體可參考CentOS7下Supervisor安裝與配置

官檔Supervisor配置

2、修改/etc/supervisord.conf文件

直接找到最下面的這個。

[include] # 這里的‘;’注釋去掉 ;files = relative/directory/*.ini files = /etc/supervisor/conf.d/*.conf # 這里加入自定義的配置文件文件路徑 

3、創建自定義文件配置文件夾

[root@localhost etc]# mkdir -p supervisor/conf.d [root@localhost etc]# cd supervisor/conf.d [root@localhost conf.d]# vim uwsgi.cof ; ================================ ; uwsgi supervisor ; ================================ [program:uwsgi] command=/usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini directory=/home/wwwroot/flask user=root stdout_logfile=/var/log/flask/uwsgi_out.log stderr_logfile=/var/log/flask/uwsgi_err.log autostart=true autorestart=true startsecs=10 priority=997 

結合自己的項目修修改改就好了。

都設置完了,下面跑起來吧。

[root@localhost supervisor]# killall supervisord # -c指定配置文件路徑,-n可前台運行,方便自己調試,具體自己去看官檔 [root@localhost supervisor]# /usr/bin/supervisord -c /etc/supervisord.conf 

然后看看跑起來沒。

[root@localhost supervisor]# ps -aux | grep supervisord root 16454 0.0 0.1 217868 10952 ? Ss 13:12 0:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf root 16633 0.0 0.0 112660 972 pts/1 S+ 13:24 0:00 grep --color=auto supervisord 

再看看uwsgi跑起來沒。

[root@localhost supervisor]# ps -aux | grep uwsgi root 16556 0.0 0.3 242740 22764 ? S 13:15 0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini root 16558 0.0 0.3 316728 18860 ? Sl 13:15 0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini root 16559 0.0 0.3 316728 18860 ? Sl 13:15 0:00 /usr/bin/uwsgi --ini /home/wwwroot/flask/uwsgi.ini root 16647 0.0 0.0 112660 972 pts/1 S+ 13:24 0:00 grep --color=auto uwsgi 

然后客戶端可以直接運行會出現命令行調試。

[root@localhost supervisor]# supervisorctl uwsgi RUNNING pid 16556, uptime 0:26:03 supervisor> help default commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version supervisor> 

有坑的話這個supervisor 啟動報錯 error: , Errno 2 No such file or directory: file: /usr/lib/python2.7/,其它就保證服務已經啟動就好了。

最后可以用虛擬環境完美解決,理論上這樣,未嘗試,因為前面pip安裝模塊后無法生成可執行命令的坑。附上參考地址virtualenv - 廖雪峰的官方網站





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM