随记-项目部署相关

python web 基础环境脚本

阿里云Ubuntu

环境: Ubuntu 16.04 + python3

1.sh

#!/bin/bash

# 更新安装源
sudo apt-get update

# 安装pip3
sudo apt-get -y install python3-pip

# 解决python版本共存问题,设置默认为python3
sudo update-alternatives --install /usr/bin/python python /usr/local/lib/python2.7 100
sudo update-alternatives --install /usr/bin/python python /usr/local/lib/python3.5 200

# 安装虚拟环境
pip3 install virtualenv
pip3 install virtualenvwrapper

# 配制虚拟环境
echo "
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace 
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
" >> ~/.bashrc
 

如果将其它命令也放入1.sh 会使虚拟环境的配制不生效,所以拆分成多个shell 脚本, 并且为了将web 应用所需要的模块安装到虚拟环境中, 也需要将

2.sh


# 安装msyql
sudo apt-get -y install mysql-server
sudo apt-get -y install libmysqlclient-dev

# 安装redis
sudo apt-get -y install redis-server
 
# 安装git
sudo apt-get -y install git
 
# 安装前置依赖,
sudo apt-get -y build-dep python-mysqldb

# 安装模块
pip3 install alembic==0.9.9
pip3 install asn1crypto==0.24.0
pip3 install attrs==18.1.0
pip3 install Automat==0.6.0
pip3 install backcall==0.1.0
pip3 install backports.shutil-get-terminal-size==1.0.0
pip3 install beautifulsoup4==4.6.0
pip3 install blinker==1.4
pip3 install bs4==0.0.1
pip3 install certifi==2018.4.16
pip3 install cffi==1.11.5
pip3 install chardet==3.0.4
pip3 install checkbox-support==0.38.0
pip3 install click==6.7
pip3 install colorama==0.3.9
pip3 install constantly==15.1.0
pip3 install cryptography==2.2.2
pip3 install cssselect==1.0.3
pip3 install decorator==4.3.0
pip3 install defer==1.0.4
pip3 install dominate==2.3.1
pip3 install enum34==1.1.6
pip3 install feedparser==5.2.1
pip3 install Flask==0.10.1
pip3 install Flask-Bootstrap==3.3.7.1
pip3 install Flask-Login==0.4.1
pip3 install Flask-Mail==0.9.1
pip3 install Flask-Migrate==2.1.1
pip3 install Flask-MySQLdb==0.2.0
pip3 install Flask-Redis==0.3.0
pip3 install Flask-Script==2.0.6
pip3 install Flask-Session==0.3.1
pip3 install Flask-SQLAlchemy==2.3.2
pip3 install Flask-Testing==0.7.1
pip3 install Flask-WTF==0.14.2
pip3 install gevent==1.3.3
pip3 install greenlet==0.4.13
pip3 install guacamole==0.9.2
pip3 install gunicorn==19.8.1
pip3 install html5lib==1.0.1
pip3 install httplib2==0.11.3
pip3 install hyperlink==18.0.0
pip3 install idna==2.6
pip3 install incremental==17.5.0
pip3 install ipaddress==1.0.22
pip3 install ipython==6.4.0
pip3 install ipython-genutils==0.2.0
pip3 install itsdangerous==0.24
pip3 install jedi==0.12.0
pip3 install Jinja2==2.10
#               pip install # jtypes.jpype==0.6.2b5
pip3 install louis==1.3
pip3 install lxml==4.2.1
pip3 install Mako==1.0.7
pip3 install MarkupSafe==1.0
pip3 install MyApplication==0.1.0
pip3 install mysqlclient==1.3.12
pip3 install oauthlib==2.1.0
pip3 install padme==1.1.1
pip3 install parsel==1.4.0
pip3 install parso==0.2.1
pip3 install pathlib2==2.3.2
pip3 install pbr==4.0.4
pip3 install pexpect==4.6.0
pip3 install pickleshare==0.7.4
pip3 install Pillow==5.1.0
pip3 install plainbox==0.38.0
pip3 install prompt-toolkit==1.0.15
pip3 install psutil==5.4.6
pip3 install ptyprocess==0.5.2
pip3 install pyasn1==0.4.3
pip3 install pyasn1-modules==0.2.1
pip3 install pycparser==2.18
#               pip install # pycurl==7.43.0.2
pip3 install PyDispatcher==2.0.5
pip3 install pygame==1.9.3
pip3 install Pygments==2.2.0
pip3 install PyJWT==1.6.4
pip3 install PyMySQL==0.8.1
pip3 install pyOpenSSL==18.0.0
pip3 install pyparsing==2.2.0
pip3 install python-dateutil==2.7.2
pip3 install python-debian==0.1.32
pip3 install python-editor==1.0.3
pip3 install python-systemd==0.0.9
pip3 install pyxdg==0.26
pip3 install qiniu==7.2.0
pip3 install queuelib==1.5.0
pip3 install redis==2.10.6
pip3 install redis-py-cluster==1.3.4
pip3 install reportlab==3.4.0
pip3 install requests==2.18.4
pip3 install requests-unixsocket==0.1.5
pip3 install scandir==1.7
pip3 install service-identity==17.0.0
pip3 install simplegeneric==0.8.1
pip3 install six==1.11.0
pip3 install SQLAlchemy==1.2.8
#               pip install # ssh-import-id==5.6
pip3 install stevedore==1.28.0
pip3 install traitlets==4.3.2
pip3 install urllib3==1.22
pip3 install virtualenv==16.0.0
pip3 install virtualenv-clone==0.3.0
pip3 install virtualenvwrapper==4.8.2
pip3 install visitor==0.1.3
pip3 install w3lib==1.19.0
pip3 install wcwidth==0.1.7
pip3 install webencodings==0.5.1
pip3 install WebOb==1.8.2
pip3 install Werkzeug==0.14.1
pip3 install WTForms==2.1
pip3 install XlsxWriter==1.0.5
pip3 install xmltodict==0.11.0
pip3 install zope.interface==4.5.0 
# 安装 gunicorn
pip3 install gunicorn

# 安装Nginx
sudo apt-get -y install nginx

# 停止Nginx 服务
/etc/init.d/nginx stop



使用说明

将.sh 文件放到服务器,
执行以下命令

# 修改sh 文件权限
chmod 764 pre_test.sh
chmod 764 test.sh

# 替换文件中的\r
sed -i 's/\r$//' 1.sh 
sed -i 's/\r$//' 2.sh

# 执行1.sh
./1.sh

# 使配置文件生效
source ~/.bashrc   
mkvirtualenv -p python3 虚拟环境名字
./2.sh
 
# 修改Nginx ,然后启动


# 配置Nginx 示例 文件 /etc/nginx/sites-available/default

"
upstream flask {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
}
server {
        # 监听80端口
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # 请求转发到gunicorn服务器
                proxy_pass http://127.0.0.1:5000;
                # 请求转发到多个gunicorn服务器
                # proxy_pass http://flask;
                # 设置请求头,并将头信息传递给服务器端 
                proxy_set_header Host $host;
                # 设置请求头,传递原始请求ip给 gunicorn 服务器
                proxy_set_header X-Real-IP $remote_addr;
        }
}

" 

阿里云Centos

CentOS 7.2 + Python3

1.sh

#!/bin/bash
# 安装python3
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
mv /usr/bin/python /usr/bin/python.bak
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure prefix=/usr/local/python3
make && make install

2.sh

#!/bin/bash
# 还原系统默认设置,保证系统正常运行
sudo sed -i "s/python/python2/" /usr/bin/yum 
sudo sed -i "s/python/python2/" /usr/libexec/urlgrabber-ext-down
 
# mysql
yum install mysql -y
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server -y
yum install mysql-devel -y
service mysqld restart

echo "
[mysql]
default-character-set=utf8
" >> /etc/my.cnf

service mysqld restart

# Redis
yum install gcc -y
wget http://download.redis.io/releases/redis-4.0.6.tar.gz

tar -zxvf redis-4.0.6.tar.gz
cd /root/Python-3.6.2/redis-4.0.6
make MALLOC=libc 
cd /root/Python-3.6.2/redis-4.0.6/src && make install
 
cd /root/Python-3.6.2/redis-4.0.6
sudo sed -i "s/daemonize no/daemonize yes/" redis.conf
cd /root/Python-3.6.2/redis-4.0.6/src 
# ./redis-server /root/Python-3.6.2/redis-4.0.6/redis.conf
 
#killall -9 redis-server
cd /etc
mkdir redis
\cp -f /root/Python-3.6.2/redis-4.0.6/redis.conf /etc/redis/6379.conf
\cp -f /root/Python-3.6.2/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd  
chmod 775 /etc/init.d/redisd

sed -i  "2a # chkconfig:   2345 90 10" /etc/init.d/redisd 
sed -i  "3a # description:  Redis is a persistent key-value database" /etc/init.d/redisd 

chkconfig redisd on

# git
sudo yum -y install git

# 虚拟环境
cd /opt
# 创建虚拟环境
python -m venv py3_flask

3.sh

#!/bin/bash
# mysql-python
sudo yum install python-devel -y
sudo yum install libevent-devel -y
sudo pip install mysql-python

pip install alembic==0.9.9
pip install asn1crypto==0.24.0
pip install attrs==18.1.0
pip install Automat==0.6.0
pip install backcall==0.1.0
pip install backports.shutil-get-terminal-size==1.0.0
pip install beautifulsoup4==4.6.0
pip install blinker==1.4
pip install bs4==0.0.1
pip install certifi==2018.4.16
pip install cffi==1.11.5
pip install chardet==3.0.4
pip install checkbox-support==0.38.0
pip install click==6.7
pip install colorama==0.3.9
pip install constantly==15.1.0
pip install cryptography==2.2.2
pip install cssselect==1.0.3
pip install decorator==4.3.0
pip install defer==1.0.4
pip install dominate==2.3.1
pip install enum34==1.1.6
pip install feedparser==5.2.1
pip install Flask==0.10.1
pip install Flask-Bootstrap==3.3.7.1
pip install Flask-Login==0.4.1
pip install Flask-Mail==0.9.1
pip install Flask-Migrate==2.1.1
pip install Flask-MySQLdb==0.2.0
pip install Flask-Redis==0.3.0
pip install Flask-Script==2.0.6
pip install Flask-Session==0.3.1
pip install Flask-SQLAlchemy==2.3.2
pip install Flask-Testing==0.7.1
pip install Flask-WTF==0.14.2
pip install gevent==1.3.3
pip install greenlet==0.4.13
pip install guacamole==0.9.2
pip install gunicorn==19.8.1
pip install html5lib==1.0.1
pip install httplib2==0.11.3
pip install hyperlink==18.0.0
pip install idna==2.6
pip install incremental==17.5.0
pip install ipaddress==1.0.22
pip install ipython==6.4.0
pip install ipython-genutils==0.2.0
pip install itsdangerous==0.24
pip install jedi==0.12.0
pip install Jinja2==2.10
#               pip install # jtypes.jpype==0.6.2b5
pip install louis==1.3
pip install lxml==4.2.1
pip install Mako==1.0.7
pip install MarkupSafe==1.0
pip install MyApplication==0.1.0
pip install mysqlclient==1.3.12
pip install oauthlib==2.1.0
pip install padme==1.1.1
pip install parsel==1.4.0
pip install parso==0.2.1
pip install pathlib2==2.3.2
pip install pbr==4.0.4
pip install pexpect==4.6.0
pip install pickleshare==0.7.4
pip install Pillow==5.1.0
pip install plainbox==0.38.0
pip install prompt-toolkit==1.0.15
pip install psutil==5.4.6
pip install ptyprocess==0.5.2
pip install pyasn1==0.4.3
pip install pyasn1-modules==0.2.1
pip install pycparser==2.18
#               pip install # pycurl==7.43.0.2
pip install PyDispatcher==2.0.5
pip install pygame==1.9.3
pip install Pygments==2.2.0
pip install PyJWT==1.6.4
pip install PyMySQL==0.8.1
pip install pyOpenSSL==18.0.0
pip install pyparsing==2.2.0
pip install python-dateutil==2.7.2
pip install python-debian==0.1.32
pip install python-editor==1.0.3
pip install python-systemd==0.0.9
pip install pyxdg==0.26
pip install qiniu==7.2.0
pip install queuelib==1.5.0
pip install redis==2.10.6
pip install redis-py-cluster==1.3.4
pip install reportlab==3.4.0
pip install requests==2.18.4
pip install requests-unixsocket==0.1.5
pip install scandir==1.7
pip install service-identity==17.0.0
pip install simplegeneric==0.8.1
pip install six==1.11.0
pip install SQLAlchemy==1.2.8
#               pip install # ssh-import-id==5.6
pip install stevedore==1.28.0
pip install traitlets==4.3.2
pip install urllib3==1.22
pip install virtualenv==16.0.0
pip install virtualenv-clone==0.3.0
pip install virtualenvwrapper==4.8.2
pip install visitor==0.1.3
pip install w3lib==1.19.0
pip install wcwidth==0.1.7
pip install webencodings==0.5.1
pip install WebOb==1.8.2
pip install Werkzeug==0.14.1
pip install WTForms==2.1
pip install XlsxWriter==1.0.5
pip install xmltodict==0.11.0
pip install zope.interface==4.5.0

sudo yum -y install nginx

sed -i  "47a# 请求转发到gunicorn服务器                                    " /etc/nginx/nginx.conf 
sed -i  "48aproxy_pass http\:\/\/127.0.0.1:5000;                          " /etc/nginx/nginx.conf 
sed -i  "49a# 请求转发到多个gunicorn服务器                                " /etc/nginx/nginx.conf 
sed -i  "50a# proxy_pass http\:\/\/flask;                                 " /etc/nginx/nginx.conf 
sed -i  "51aproxy_set_header Host \$host;                                 " /etc/nginx/nginx.conf 
sed -i  "52aproxy_set_header X-Real-IP \$remote_addr;                     " /etc/nginx/nginx.conf 
sed -i  "53aproxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; " /etc/nginx/nginx.conf 
sed -i  "54aproxy_set_header Cookie \$http_cookie;                        " /etc/nginx/nginx.conf 

pip install gunicorn

# shutdown -r now
service redisd start
service nginx start

使用说明


# 修改权限
chmod 764 1.sh
sed -i 's/\r$//' 1.sh
chmod 764 2.sh
sed -i 's/\r$//' 2.sh
chmod 764 3.sh
sed -i 's/\r$//' 3.sh

# 执行1.sh
.1.sh

# 执行完成, 执行一下3条命令
\rm -f /usr/bin/python
ln -s /usr/local/python3/bin/python3 /usr/bin/python
.2.sh

# 执行完成, 执行一下2条命令
source /opt/py3_flask/bin/activate
.3.sh




# redis 服务 启动/停止命令 
service redisd start
service redisd stop

# nginx 服务
service nginx restart
service nginx start
service nginx stop

# 注意点

在win下编辑的时候,换行结尾是\n\r , 而在linux下 是\n,所以才会有 多出来的\r

sed -i 's/\r$//' 文件名(假设是 build.sh)

会把 build.sh 中的\r 替换成空白!
再次编译!成功!!


centos mysql 安装完成无密码

第一步:用帐号登录mysql
[root@CentOs5 ~]# mysql -uroot
第二步:改变用户数据库
mysql> use mysql
第三步:修改密码,记得密码要用password()函数进行加密,一定不要忘记!!!
mysql> update user set password=password("mysql") where User="root";
第四步:刷新权限表
mysql> flush privileges;
第五步:退出mysql,对mysql进行重启
[root@CentOs5 ~]# service mysqld restart;


----
到此结  DragonFangQy 2018.7.4

你可能感兴趣的:(随记-项目部署相关)