什么是uwsgi?
uWSGI是一個Web服務器,它實現(xiàn)了WSGI協(xié)議、uwsgi、http等協(xié)議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規(guī)范。
在開始之前
最小化安裝CentOS 6
備份網卡文件
~$ mkdir /etc/sysconfig/network-scripts/backup~$ cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/backup/ifcfg-eth0.backup
配置阿里云鏡像源
~$ mkdir /etc/yum.repos.d/old~$ mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/old/~$ cd /etc/yum.repos.d/~$ curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo~$ yum clean all && yum repolist all && yum update -y~$ reboot
Python3.6.0
上傳Python-3.6.0.tar.xz
~$ rz
安裝依賴
yum install zlib* gcc openssl openssl-devel libffi-devel -yyum install pcre pcre-devel pcre-static -y
解壓Python-3.6.0.tar.xz
~$ tar -xvf Python-3.6.0.tar.xz~$ cd Python-3.6.0
修改部分源代碼
~$ vim Modules/Setup.dist# 將該文件的204到209行部分代碼取消注釋,完成后如下所示:# Socket module helper for socket(2)_socket socketmodule.c# Socket module helper for SSL support; you must comment out the other# socket line above, and possibly edit the SSL variable:SSL=/usr/local/ssl_ssl _ssl.c / -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl / -L$(SSL)/lib -lssl -lcrypto# The crypt module is now disabled by default because it breaks builds
編譯安裝
~$ ./configure~$ make -j~$ make install~$ cd~$ rm -rf Python-3.6.0
防火墻
# 恢復默認配置iptables -F# 放通3306/8000/80端口iptables -I INPUT -p tcp -m tcp --dport 3306 -j ACCEPTiptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPTiptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT# 保存規(guī)則/etc/init.d/iptables save
SELinux
關閉SELinux
~$ vim /etc/selinux/config# 修改配置為如下所示:SELINUX=permissive~$ reboot
數(shù)據(jù)庫
二進制方式安裝
# 查找相關舊文件并刪除find / -name mysqlfind / -name mariadb# 移除全部相關包rpm -qa | grep mysqlrpm -qa | grep mariadb# 添加用戶useradd mysql -s /sbin/nologin -M# 解壓移動文件tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gzmv mysql-5.7.24-linux-glibc2.12-x86_64 /applications/ln -s /applications/mysql-5.7.24-linux-glibc2.12-x86_64/ /applications/mysql# 創(chuàng)建配置文件vim /etc/my.cnf# 創(chuàng)建相關目錄mkdir -p /data/mysql/datamkdir -p /data/mysql/log# 手動創(chuàng)建日志文件touch /data/mysql/log/mysqld.log# 修改權限chown -R mysql.mysql /applications/mysqlchown -R mysql.mysql /data/mysql
MySQL配置文件
[client]port=3306socket=/data/mysql/mysql.sock[mysqld]port=3306datadir=/data/mysql/databasedir=/applications/mysqlpid-file=/data/mysql/mysqld.pidsocket=/data/mysql/mysql.sockuser=mysqlcharacter-set-server=utf8mb4default-storage-engine=INNODBcollation-server = utf8mb4_general_ciinit_connect='SET NAMES utf8mb4'max_connections = 1000max_connect_errors = 1200max_allowed_packet = 128Mexplicit_defaults_for_timestamp = truequery_cache_size = 0query_cache_type = 0log_error = /data/mysql/log/error.logslow_query_log = 1slow_query_log_file = /data/mysql/log/slow.loglog_queries_not_using_indexes = 1log_throttle_queries_not_using_indexes = 5long_query_time = 8log_slow_slave_statements = 1min_examined_row_limit = 100expire_logs_days = 5tmpdir = /tmpinnodb_buffer_pool_size = 128M[mysqld_safe]log-error=/data/mysql/log/mysqld.logpid-file=/data/mysql/mysqld.pid
# 同步數(shù)據(jù)/applications/mysql/bin/mysql_install_db --basedir=/applications/mysql/ --datadir=/data/mysql/data/ --user=mysql
配置并啟動
cp /applications/mysql/support-files/mysql.server /etc/init.d/mysqldchmod +x /etc/init.d/mysqld vim /etc/init.d/mysqld
# 修改以下兩行basedir=/applications/mysqldatadir=/data/mysql/data
# 查看是否啟動netstat -tunlap | grep mysql# 添加服務并設置為開機自啟動chkconfig --add mysqldchkconfig mysqld on
初始化數(shù)據(jù)庫
/applications/mysql/bin/mysql_secure_installation
-- 設置用戶密碼alter user 'root'@'localhost' identified by '123456';-- 允許root遠程訪問GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;FLUSH PRIVILEGES;
Django
配置pip3源
mkdir /root/.piptouch /root/.pip/pip.confecho '[global]' >> /root/.pip/pip.confecho 'trusted-host=mirrors.aliyun.com' >> /root/.pip/pip.confecho 'index-url=https://mirrors.aliyun.com/pypi/simple/' >> /root/.pip/pip.conf
創(chuàng)建虛擬環(huán)境安裝依賴
# PublisherPro,一個支持MD輕量級的CMS程式.git clone https://gitee.com/bluemiaomiao/PublisherPro.gitpip3 install virtualenvcd PROJECT_DIRvirtualenv venvsource venv/bin/activatepip3 install -r requestments.txtpip3 install uwsgimkdir logmkdir scripttouch PublisherPro/script/uwsgi.pidtouch PublisherPro/script/uwsgi.statusvim uwsgi.ini
修改項目配置
# PROJECT_DIR/PROJECT_NAME/settings.py# 設置為生產環(huán)境DEBUG = False# 配置數(shù)據(jù)庫DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'publisher_pro', 'USER': 'pubpro', 'PASSWORD': 'bluemiaomiao', 'HOST': '192.168.1.203', 'PORT': '3306', 'OPTIONS': {'init_command': 'SET default_storage_engine=INNODB;'}, }}# 配置靜態(tài)文件相關# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]STATIC_ROOT = os.path.join(BASE_DIR, 'static')創(chuàng)建數(shù)據(jù)庫和用戶
CREATE DATABASE `publisher_pro` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';CREATE USER `pubpro`@`localhost` IDENTIFIED BY 'bluemiaomiao' PASSWORD EXPIRE NEVER;CREATE USER `pubpro`@`%` IDENTIFIED BY 'bluemiaomiao' PASSWORD EXPIRE NEVER;GRANT All ON `publisher/_pro`.* TO `pubpro`@`%`;
同步數(shù)據(jù)庫
./venv/bin/python3 manage.py makemigrations./venv/bin/python3 manage.py migrate./venv/bin/python3 manage.py createsuperuser./venv/bin/python3 manage.py collectstatic
uwsgi
配置文件內容
# uwsig使用配置文件啟動[uwsgi]# 項目目錄chdir=/applications/website/PublisherPro# 指定項目的applicationmodule=PublisherPro.wsgi:application# 指定sock的文件路徑 socket=/applications/website/PublisherPro/script/uwsgi.sock# 進程個數(shù) workers=5pidfile=/applications/website/PublisherPro/script/uwsgi.pid# 狀態(tài)文件stats=/applications/website/PublisherPro/script/uwsgi.status# 指定IP端口 http=0.0.0.0:8000# 指定靜態(tài)文件static-map=/static=/applications/website/PublisherPro/static# 啟動uwsgi的用戶名和用戶組uid=pubprogid=pubpro# 啟用主進程master=true# 自動移除unix Socket和pid文件當服務停止的時候vacuum=true# 序列化接受的內容,如果可能的話thunder-lock=true# 啟用線程enable-threads=true# 設置自中斷時間harakiri=30# 設置緩沖post-buffering=4096# 設置日志目錄daemonize=/applications/website/PublisherPro/log/uwsgi.log
創(chuàng)建用戶和組并修改權限
# 創(chuàng)建用戶useradd pubpro -s /sbin/nologin -M# 檢查結果id pubpro# 修改權限chown -R pubpro.pubpro /applications/website/PublisherPro/# 檢查結果ll -d /applications/website/PublisherPro/
測試Django應用
# 啟動應用uwsgi --ini uwsgi.ini# 重載應用uwsgi --reload script/uwsgi.pid# 狀態(tài)信息uwsgi --connect-and-read script/uwsgi.status# 停止應用uwsgi --stop script/uwsgi.pid
Nginx
server { listen 80; server_name 192.168.2.108; access_log /var/log/nginx/access.log main; charset utf-8; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定項目路徑uwsgi location / { # 導入一個Nginx模塊他是用來和uWSGI進行通訊的 include uwsgi_params; # 設置連接uWSGI超時時間 uwsgi_connect_timeout 30; # 指定uwsgi的sock文件所有動態(tài)請求就會直接丟給他 uwsgi_pass unix:/data/PublisherPro/script/uwsgi.sock; } # 指定靜態(tài)文件路徑 location /static/ { alias /data/PublisherPro/static; index index.html index.htm; }}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答
圖片精選