0-python基础之-环境搭建

window下安装


1.安装python

下载python-3.7.0-amd64.exe,直接下一步安装,记得勾选Add Python 3.7 to PATH
https://www.python.org/downloads/release/python-370/

2.安装web server需要的库和服务

1.安装mysql,查看mysql的安装教程

安装完测试正常后,使用以下命令部署web的后台数据库框架
mysql -u root -p < schema.sql

2.pip3 install jinja2 aiomysql aiohttp

3.安装pyCharm64

win上面开发一定要有集成开发环境,使用pyCharm很方便。

下载专业版,进行破解,这样就可以写css,js的代码了
https://blog.csdn.net/u014044812/article/details/78727496

K71U8DBPNE-eyJsaWNlbnNlSWQiOiJLNzFVOERCUE5FIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJSUzAiLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJXUyIsInBhaWRVcFRvIjoiMjAxOS0wNS0wNCJ9LHsiY29kZSI6IlJEIiwicGFpZFVwVG8iOiIyMDE5LTA1LTA0In0seyJjb2RlIjoiUkMiLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJEQyIsInBhaWRVcFRvIjoiMjAxOS0wNS0wNCJ9LHsiY29kZSI6IkRCIiwicGFpZFVwVG8iOiIyMDE5LTA1LTA0In0seyJjb2RlIjoiUk0iLCJwYWlkVXBUbyI6IjIwMTktMDUtMDQifSx7ImNvZGUiOiJETSIsInBhaWRVcFRvIjoiMjAxOS0wNS0wNCJ9LHsiY29kZSI6IkFDIiwicGFpZFVwVG8iOiIyMDE5LTA1LTA0In0seyJjb2RlIjoiRFBOIiwicGFpZFVwVG8iOiIy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SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==

Centos下安装


1.安装python

编译时要提前装好gcc编译器和zlib zlib-devel

下载Python安装包

cd /usr/local/src
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz

2、解压

tar -zxvf Python-3.7.0.tgz

3、编译安装包

cd Python-3.7.0
./configure --prefix=/usr/local/python
make && make install

4、添加环境变量

#echo PATH='/usr/local/python/bin/:$PATH' >> /etc/profile

#source /etc/profile

有时候会出现如下错误

ModuleNotFoundError: No module named '_ctypes'

安装如下即可:

yum install libffi-devel -y

5.检查是否成功,执行下面代码

python3.7
Python 3.7.0 (default, Nov 27 2018, 10:35:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
2.修改默认python版本(这一步可能不需要,因为会导致supervisorctl启动不了,supervisorctl只能要python2上面跑)

1.查看默认python版本是2.7.5

python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

2.修改系统默认的Python路径,因为在终端中输入Python命令时默认是指向Python2.7.5

mv /usr/bin/python /usr/bin/python-2.7.5
``` 

3.建立新的软连接,指向Python-3.7.0  

ln -s /usr/local/python/bin/python3.7 /usr/bin/python
 ```

4.打开一个新的终端,通过python命令进入python环境,可以看到已经指向了我们新安装的python3.7.0:

python
Python 3.7.0 (default, Nov 27 2018, 10:35:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("test")
test
>>> exit()

5.改完默认python后发现使用yum安装时出错

 yum install openssh-server
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

6.因为yum是依赖python的,所以这里我们修改了默认的python,就要要修改yum,让其运行指向旧的版本:

vi /usr/bin/yum   

将第一行中的“#!/usr/bin/python”   
修改为“#!/usr/bin/python-2.7.5”
保存即可

7.如果出现别的错误的时候有可以需要修改一下两个文件

yum install tree
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================
 Package                Arch                     Version                        Repository              Size
=============================================================================================================
Installing:
 tree                   x86_64                   1.6.0-10.el7                   base                    46 k

Transaction Summary
=============================================================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]: y
Downloading packages:
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax


Exiting on user cancel

1. vim /usr/libexec/urlgrabber-ext-down

将/usr/bin/python改为/usr/bin/python-2.7.5。

2. vim /usr/bin/yum-config-manager

解决办法同上: #!/usr/bin/python换成 #!/usr/bin/python-2.7.5 
3.安装web server需要的库和服务

1.安装mysql,查看mysql的安装教程

安装完测试正常后,使用以下命令部署web的后台数据库框架
mysql -u root -p < schema.sql

2.pip3 install jinja2 aiomysql aiohttp

3.yum install openssh-server nginx supervisor dos2unix

这边用到下面两个工具

  • 1.Supervisor:监控服务进程的工具;

该工具的主要目的就是使用Supervisor来启动我们的app.py进程,这样app.py进程出现问题时就会被保存日志重启等。

使用echo_supervisord_conf产生默认配置文件,保存在supervisord.conf里面

echo_supervisord_conf > /etc/supervisord.conf

修改/etc/supervisord.conf的最后两行

[include]
files = /etc/supervisord.d/*.conf

在/etc/supervisord.d/里面添加配置文件awesome.conf

[program:awesome]
command     = /srv/awesome/www/app.py
directory   = /srv/awesome/www
user        = root
startsecs   = 3
redirect_stderr         = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 10
stdout_logfile          = /srv/awesome/log/app.log

查看要运行的www属于谁的权限,就将上面awesome.conf里面的user配置成谁

[root@centos awesome]# ls -l
total 24
drwxr-xr-x 2 root root 4096 Nov 27 17:23 log
lrwxrwxrwx 1 root root   21 Nov 27 16:56 www -> www-18-11-27_16.56.28
drwxr-xr-x 4 root root 4096 Nov 27 16:09 www-18-11-27_16.08.59
drwxr-xr-x 4 root root 4096 Nov 27 16:26 www-18-11-27_16.26.12
drwxr-xr-x 4 root root 4096 Nov 27 16:28 www-18-11-27_16.28.37
drwxr-xr-x 4 root root 4096 Nov 27 16:53 www-18-11-27_16.53.41
drwxr-xr-x 5 root root 4096 Nov 27 17:11 www-18-11-27_16.56.28

参考:https://www.cnblogs.com/wswang/p/5795766.html

supervisorctl stop program_name  # 停止某一个进程,program_name 为 [program:x] 里的 x

supervisorctl start program_name  # 启动某个进程

supervisorctl restart program_name  # 重启某个进程

supervisorctl stop groupworker:  # 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理)

supervisorctl stop groupworker:name1  # 结束 groupworker:name1 这个进程 (start,restart 同理)

supervisorctl stop all  # 停止全部进程,注:start、restartUnlinking stale socket /tmp/supervisor.sock
、stop 都不会载入最新的配置文件

supervisorctl reload  # 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程

supervisorctl update  # 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
[root@centos awesome]# supervisorctl stop awesome
awesome: stopped
[root@centos awesome]# supervisorctl start awesome
awesome: started
[root@centos awesome]# supervisorctl status
awesome                          RUNNING   pid 30182, uptime 0:00:13

测试下supervisord进程是否生效,找到app.py的进程kill掉,可以发现出现了一个新id的app.py进程

[root@centos awesome]# ps -aux | grep python
root     11340  0.0  1.4 573812 14660 ?        Ssl  Nov23   0:39 /usr/bin/python -Es /usr/sbin/tuned -l -P
root     30161  0.0  1.6 225936 17072 ?        Ss   17:23   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     30182  0.0  2.6 250196 26612 ?        S    17:28   0:00 python3 /srv/awesome/www/app.py
root     30350  0.0  0.0 112704   972 pts/0    R+   18:00   0:00 grep --color=auto python
[root@centos awesome]# kill 30182
[root@centos awesome]# ps -aux | grep python
root     11340  0.0  1.4 573812 14660 ?        Ssl  Nov23   0:39 /usr/bin/python -Es /usr/sbin/tuned -l -P
root     30161  0.0  1.6 225936 17068 ?        Ss   17:23   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root     30364  0.0  2.6 249844 26440 ?        S    18:01   0:00 python3 /srv/awesome/www/app.py
root     30366  0.0  0.0 112704   976 pts/0    R+   18:01   0:00 grep --color=auto python


  • 2.Nginx:高性能Web服务器+负责反向代理;

利用Python自带的asyncio,我们已经编写了一个异步高性能服务器。但是,我们还需要一个高性能的Web服务器,这里选择Nginx,它可以处理静态资源,同时作为反向代理把动态请求交给Python代码处理

动静结合,我们的python服务器属于动态的,会修改;Nginx属于静态的,为代理作用,更加安全,比如python服务器出现问题是,不会因为疏忽而把debug信息直接输出给客户端,这时候就有Nginx服务器直接返回502错误给客户端。

[图片上传失败...(image-a12c23-1581845045050)]

[root@centos ~]# ps -aux | grep nginx
root       737  0.0  0.0 112704   968 pts/0    S+   10:00   0:00 grep --color=auto nginx
root     30865  0.0  0.2 120792  2100 ?        Ss   Nov27   0:00 nginx: master process nginx
nginx    30866  0.0  0.3 121180  3584 ?        S    Nov27   0:00 nginx: worker process

systemctl restart nginx.service/service nginx restart

systemctl stop nginx.service/service nginx stop

systemctl start nginx.service/service nginx start

systemctl status nginx.service/service nginx status

在阿里云服务器上由于没有界面也不知道nginx是否正常启动,这时候可以使用curl去过去网页内容,然后再复制出来到win上面看html页面是否显示正常。

如下:

[root@centos nginx]# curl http://127.0.0.1



    
        Test Page for the Nginx HTTP Server on Fedora
        
        
    

    
        

Welcome to nginx on Fedora!

This page is used to test the proper operation of the nginx HTTP server after it has been installed. If you can read this page, it means that the web server installed at this site is working properly.

Website Administrator

This is the default index.html page that is distributed with nginx on Fedora. It is located in /usr/share/nginx/html.

You should now put your content in a location of your choice and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf.

[ Powered by nginx ] [ Powered by Fedora ]

如果在本机可以正常访问,外网不能访问的时候,需要看下阿里云上面的安全组配置,是否有开放80端口,443端口。

https://blog.csdn.net/lzl18918615216/article/details/80049471

https://helpcdn.aliyun.com/document_detail/25471.html

如果还是不行可能需要配置防火墙规则。

https://blog.csdn.net/lzl18918615216/article/details/80049471

Ubuntu下安装


1.安装python

1、apt-get upgrade

2、apt-get update

直接安装python3.5吧,发现3.6下安装distribute有问题

3、apt-get install python3.6

4、apt-get install python3.6-dev

ubuntu默认已经安装了python3.4

root@ubuntu:/usr/bin# python3 -V
Python 3.4.3

所以我们需要把默认的改成python3.6

设置python的默认版本:

root@ubuntu:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
root@ubuntu:/usr/bin# update-alternatives --list python
/usr/bin/python2.7
root@ubuntu:/usr/bin# python -V
Python 2.7.6

设置python3的默认版

root@ubuntu:/usr/bin# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python3 (python3) in auto mode
root@ubuntu:/usr/bin# update-alternatives --list python3
/usr/bin/python3.6
root@ubuntu:/usr/bin# python3 -V
Python 3.6.2

5、设置pip:

  • apt-get install python3-pip    # Python3
  • apt-get install python-pip     # Python2

6、查看pip3 -V

root@ubuntu:/usr/bin# pip3
Traceback (most recent call last):
  File "/usr/bin/pip3", line 5, in 
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 1479, in 
    register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'

AttributeError:module 'importlib._bootstrap' has no attribute 'SourceFileLoader'。

最后发现:distribute只能安装在python 3.5.x版本。

7、安装apt-get install python3、apt-get install python3.5-dev切换到3.5

root@ubuntu:/usr/bin# update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.6   1         auto mode
  1            /usr/bin/python3.5   1         manual mode
* 2            /usr/bin/python3.6   1         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python3 (python3) in manual mode
root@ubuntu:/usr/bin# pip3 -V
pip 1.5.4 from /usr/lib/python3/dist-packages (python 3.5)

8、更新pip3

pip3 install --upgrade setuptools
2.安装web server需要的库和服务

1.安装mysql,查看mysql的安装教程

安装完测试正常后,使用以下命令部署web的后台数据库框架
mysql -u root -p < schema.sql

2.pip3 install jinja2 aiomysql aiohttp

pip3 install idna_ssl cryptography

3.apt-get install libssl-dev openssh-server nginx supervisor dos2unix

4.配置Supervisor:监控服务进程的工具

编写一个Supervisor的配置文件awesome.conf,存放到/etc/supervisor/conf.d/目录下:

[program:awesome]

command     = /srv/awesome/www/app.py
directory   = /srv/awesome/www
user        = root
startsecs   = 3

redirect_stderr         = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups  = 10
stdout_logfile          = /srv/awesome/log/app.log

在srv下面创建awesome文件夹。

使用fabfile_win.py将项目同步到ubuntu,记得修改ip和用户名。

env.user = 'linye'
env.sudo_user = 'root'
# env.hosts = ['123.123.123.123']
env.host_string = '192.168.61.128' # 改成你的服务器ip

创建/srv/awesome/log/文件夹

service supervisor start

出现unix:///var/run/supervisor.sock no such file是就自己创建一个,并附权限

touch /var/run/supervisor.sock
chmod 777 /var/run/supervisor.sock

5.Nginx:高性能Web服务器+负责反向代理;

/etc/nginx/sites-available/default里面的内容改成如下:

server {
    listen      80; # 监听80端口

    root       /srv/awesome/www;
    access_log /srv/awesome/log/access_log;
    error_log  /srv/awesome/log/error_log;

    # server_name awesome.liaoxuefeng.com; # 配置域名

    # 处理静态文件/favicon.ico:
    location /favicon.ico {
        root /srv/awesome/www;
    }

    # 处理静态资源:
    location ~ ^\/static\/.*$ {
        root /srv/awesome/www;
    }

    # 动态请求转发到9000端口:
    location / {
        proxy_pass       http://127.0.0.1:9000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

/etc/init.d/nginx reload

你可能感兴趣的:(0-python基础之-环境搭建)