centos7安裝python,mariaDB,nginx,django,flask

0,安裝centos7html

centos默認不開啓網卡,須要在安裝時將ens33設置爲on,或者後續經過vi ifcfg-ens33,找到onboot,設置爲yes
python

或者也能夠更名:cd /etc/sysconfig/network-scripts/   mv ifcfg-ens33 ifcfg-eth0, vi ifcfg-eth0(將name改成eth0)mysql

ssh登錄centos7時,若是提示WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!,能夠用ssh-keygen -R  [IP_ADDRESS]重置nginx

 

 

1,安裝python3c++

關閉yum(若是在運行):rm -f /var/run/yum.pidsql

安裝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-develdjango

安裝依賴包(python3.7的pip須要):yum install libffi-devel -yflask

查看最新版本:https://www.python.org/ftp/python/vim

安裝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

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf    # 加載配置文件啓動,也能夠加環境變量後直接nginx -c xxx

 

cd /usr/local/nginx/sbin/

nginx -s stop

nginx -s quit

nginx -s reload

 

加環境變量:

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

firewall-cmd --reload

 

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 {
  listen 5050;
  server_name data.migelab.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 
  }   
}

開啓nginx:

nginx -c /usr/local/nginx/conf/nginx.conf 

或者可能路徑是nginx -c /etc/nginx/nginx.conf

相關文章
相關標籤/搜索