centos7安装python,mariaDB,nginx,django,flask
0,安装centos7
centos默认不开启网卡,需要在安装时将ens33设置为on,或者后续通过vi ifcfg-ens33,找到onboot,设置为yes
或者也可以改名:cd /etc/sysconfig/network-scripts/ mv ifcfg-ens33 ifcfg-eth0, vi ifcfg-eth0(将name改为eth0)
ssh登陆centos7时,如果提示WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!,可以用ssh-keygen -R [IP_ADDRESS]重置
1,安装python3
关闭yum(如果在运行):rm -f /var/run/yum.pid
安装gcc:yum install gcc
安装依赖包:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
安装依赖包(python3.7的pip需要):yum install libffi-devel -y
查看最新版本:https://www.python.org/ftp/python/
安装wget(如果没有安装):yum -y install wget
下载最新版本:wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
新建目录:mkdir /usr/local/python3
移动:mv Python-3.7.2.tgz /usr/local
进入/usr/local目录解压:tar -xvf Python-3.7.2.tgz
进入解压文件夹:cd /usr/local/Python-3.7.2
配置安装目录:./configure --prefix=/usr/local/python3(如果提示"make:*** No targets specified and no makefile found.Stop.",需要yum update , yum install gcc build-essential)
编译:make
安装:make install
删除安装包:rm Python-3.7.2.tgz
删除文件夹: rm -rf Python-3.7.2
配置python3软链接: ln -s /usr/local/python3/bin/python3 /usr/bin/python3
配置pip软链接:ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
2,安装mariaDB
安装mariaDB:yum install mariadb mariadb-server -y
安装MySQL-python:yum install MySQL-python -y
启动mariaDB:systemctl start mariadb
开机自启动:systemctl enable mariadb.service
初始化mariaDB:mysql_secure_installation
创建数据库:
mysql -uroot -ptest create database mysite;
3,安装nginx
3.1,需要先安装(如果没安装):
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
3.2,下载和安装nginx:
查看版本:https://nginx.org/en/download.html 查看版本
下载:wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
解压:tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure
make
mkdir /usr/local/nginx
make install
3.3,启动停止nginx
cd /usr/local/nginx/sbin/
nginx -c /usr/local/nginx/conf/nginx.conf # 开启
nginx -s stop # 停止
nginx -s quit
nginx -s reload # 重启
也可能是cd /etc/nginx,然后nginx -c /etc/nginx/nginx.conf
加环境变量:
vim /etc/profile,在文件最后加上:
PATH=$PATH:/usr/local/nginx/sbin
export PATH
4,创建django项目
cd / mkdir data cd /data/ find ./ -name django-admin # 找到django-admin所在目录 python3 /usr/local/python3/bin/django-admin.py startproject mysite # 创建mysite站点
修改配置文件,/data/mysite/mysite/settings.py
""" Django settings for mysite project. Generated by 'django-admin startproject' using Django 1.11.7. For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'm4@g1=hz^08y(9d)v5l!8^*0wbla=oe15s@u8@5^pw=llfz48%' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'mysite.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mysite', 'PASSWORD':'test', 'USER': 'root', 'HOST':'127.0.0.1', 'PORT':'3306', } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/'
安装pymysql:
pip install pymysql
修改data/mysite/mysite/__init__.py文件:
import pymysql pymysql.install_as_MySQLdb()
创建django数据库
cd /data/mysite python3 manage.py migrate
启动django
python3 manage.py runserver
5,FLASK
5.1,直接开启服务
app.run(host='0.0.0.0', port=5000)
python3 app01.py
备注:可能centos需要让防火墙放行端口,增加如下配置:
firewall-cmd --zone=public --add-port=5000/tcp --permanent
5.2,通过uwsgi启动flask服务
pip install uwsgi
find / -name uwsgi # /usr/local/python3/bin/uwsgi
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi # 软链接
uwsgi --socket 0.0.0.0:5000 --protocol=http -p 3 -w run:app
socket 0.0.0.0:5000:暴露端口号5000
--protocol=http:http
-p 3 : 占3个进程
-w run:app:-w 指明了要启动的模块,run 就是项目启动文件 run.py 去掉扩展名,app 是 run.py 文件中的变量 app,即 Flask 实例
5.3,配置uwsgi文件,通过nginx起服务
必须先启动uwsgi,再启动nginx
配置uwsgi,可以放在项目目录下(部署时去掉#后面注释):
[uwsgi] socket = 127.0.0.1:5000 # uWSGI 的监听端口 chdir = /home/bridge/flasksite # 项目根目录 wsgi-file = app01.py # Flask 项目的启动文件 callable = app # 程序内启用的application变量名 processes = 4 # 进程数量 vacuum = true master = true stats = %(chdir)/uwsgi/uwsgi.status # 记录uwsgi状态 pidfile = %(chdir)/uwsgi/uwsgi.pid # 记录uwsgi的pi
假设flask项目下的uwsgi结构如下:
flasksite/
|-- uwsgi.ini
|-- app01.py
|
|-- uwsgi/
| | -- uwsgi.pid
| | -- uwsgi.status
uwsgi命令:
cd flasksite
启动:uwsgi uwsgi.ini
重启:uwsgi --reload uwsgi/uwsgi.pid
关闭:uwsgi --stop uwsgi/uwsgi.pid
关闭所有uwsgi:pkill -f uwsgi -9
配置nginx,本例配置文件在/usr/local/nginx/conf/nginx.conf ,增加下面一段:
# server_name是对http访问请求头中的host进行检查
# 匹配任何不符合后续server_name配置的访问, 直接返回501错误
server {
listen 5050 default_server;
server_name _;
return 501;
}
# 配置监听5050端口
server { listen 5050; server_name mysite.com; charset utf-8; client_max_body_size 75M; location / { include uwsgi_params; # 导入uwsgi配置 uwsgi_pass 127.0.0.1:5000; # 转发端口,需要和uwsgi配置当中的监听端口一致 uwsgi_param UWSGI_PYTHON /usr/bin/python3; # Python解释器所在的路径,如果有虚拟环境可将路径设置为虚拟环境 uwsgi_param UWSGI_CHDIR /home/bridge/flasksite; # 项目根目录 uwsgi_param UWSGI_SCRIPT app02:app; # 项目的主程序,比如你测试用run.py文件,文件中app = Flask(__name__),那么这里就填run:app } }