django+uwsgi+Nginx 部署web項目

centos7下部署Django(nginx+uwsgi+python3+django)

1. yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
(安裝這些模塊都是爲了成功編譯安裝python3,防止出現各種異常)

2. yum install libxml*
(安裝這個模塊是爲了讓uwsig支持使用“-x"選項,能通過xml文件啓動項目)

3. ln -s /usr/python3/bin/pip3 /usr/bin/pip3
(以上ln命令是爲了方便在終端中直接使用pip3命令)

4. pip3 install django   ##出錯了,SSL module is not available."

安裝下面的模塊
yum install zlib-devel bzip2-devel openssl-devel

5. 重新執行python3編譯安裝後, 可以成功運行
 pip3 install django

*(yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel)# 這一行放在這裏是以防以後用得上*


6. 安裝uwsgi
 pip3 install uwsgi

7. 建立軟件鏈接
 ln -s /usr/python3/bin/uwsgi /usr/bin/uwsgi3
8. Centos 7 安裝7z, 用於解壓傳進來的項目文件和數據庫文件
wget http://nchc.dl.sourceforge.net/project/p7zip/p7zip/9.20.1/p7zip_9.20.1_src_all.tar.bz2 
? tar -jxvf p7zip_9.20.1_src_all.tar.bz2 
? cd p7zip_9.20.1 
? make 
? make install 


將你的django項目放到你想放的路徑下,例如/home/www/,假設我們的Django項目名爲"myproject",裏面有一個應用叫"myapp"
在你的django項目下新建 myproject.xml,內容如下:
<uwsgi>
    <socket>127.0.0.1:8997</socket><!-- 內部端口,自定義 -->
        <chdir>/home/www/myproject</chdir><!-- 項目路徑 -->
            <module>myproject.wsgi</module>
                <processes>4</processes> <!-- 進程數 -->
    <daemonize>uwsgi.log</daemonize><!-- 日誌文件 -->
</uwsgi>


9. 安裝Nginx

進入/usr/local/src目錄,執行以下命令:
wget http://nginx.org/download/nginx-1.13.7.tar.gz

下載完成後,執行解壓命令:
tar -zxvf nginx-1.13.7.tar.gz
進入解壓後的nginx-1.13.7文件夾,依次執行以下命令:
./configure
make
make install

10. 配置nginx.conf文件
nginx一般默認安裝好的路徑爲/usr/local/nginx
在/user/local/nginx/conf/中打開nginx.conf,加入以下內容
server {
    listen 8996; #暴露給外部訪問的端口
    server_name localhost;
        charset utf-8;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8997; #外部訪問8996就轉發到內部8997
    }
    location /static/ {
        alias /home/www/myproject/myapp/static/; #項目靜態路徑設置
    }
}

 

以下是it.settings.py

"""
Django settings for it project.

Generated by 'django-admin startproject' using Django 2.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/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/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ito-!@5o-u2!0hz0zkl+466o6zm_s7(3&qo5n4b7z&d@)3ny!f'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'deviceman',
    'account',
]

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 = 'it.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],
        '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 = 'it.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'deviceman',
        'USER': 'root',
        'PASSWORD': 'Aecom#1216',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/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/2.0/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/2.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = '/home/www/static'

 

 

以下是nginx.conf文件


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


server {
    listen 8996; #暴露給外部訪問的端口
    server_name 127.0.0.1;
        charset utf-8;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8997; #外部訪問8996就轉發到內部8997
    }
    location /static/ {
      alias /home/www/static/; #項目靜態路徑設置
       #root /home/www/it/;
    }
}

 

 


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章