您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Django+uwsgi+Nginx上線的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Django+uwsgi+Nginx上線的示例分析”這篇文章吧。
什么是uwsgi?
uWSGI是一個(gè)Web服務(wù)器,它實(shí)現(xiàn)了WSGI協(xié)議、uwsgi、http等協(xié)議。Nginx中HttpUwsgiModule的作用是與uWSGI服務(wù)器進(jìn)行交換。WSGI是一種Web服務(wù)器網(wǎng)關(guān)接口。它是一個(gè)Web服務(wù)器(如nginx,uWSGI等服務(wù)器)與web應(yīng)用(如用Flask框架寫的程序)通信的一種規(guī)范。
WSGI是一種通信協(xié)議。
uwsgi是一種線路協(xié)議而不是通信協(xié)議,在此常用于在uWSGI服務(wù)器與其他網(wǎng)絡(luò)服務(wù)器的數(shù)據(jù)通信。uwsgi協(xié)議是一個(gè)uWSGI服務(wù)器自有的協(xié)議,它用于定義傳輸信息的類型(type of information),每一個(gè)uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣?xùn)|西。
uWSGI是實(shí)現(xiàn)了uwsgi和WSGI兩種協(xié)議的Web服務(wù)器。
在開始之前
最小化安裝CentOS 6
備份網(wǎng)卡文件
~$ 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 -y yum 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
防火墻
# 恢復(fù)默認(rèn)配置 iptables -F # 放通3306/8000/80端口 iptables -I INPUT -p tcp -m tcp --dport 3306 -j ACCEPT iptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPT iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT # 保存規(guī)則 /etc/init.d/iptables save
SELinux
關(guān)閉SELinux
~$ vim /etc/selinux/config # 修改配置為如下所示: SELINUX=permissive ~$ reboot
數(shù)據(jù)庫
二進(jìn)制方式安裝
# 查找相關(guān)舊文件并刪除 find / -name mysql find / -name mariadb # 移除全部相關(guān)包 rpm -qa | grep mysql rpm -qa | grep mariadb # 添加用戶 useradd mysql -s /sbin/nologin -M # 解壓移動(dòng)文件 tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz mv 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)建相關(guān)目錄 mkdir -p /data/mysql/data mkdir -p /data/mysql/log # 手動(dòng)創(chuàng)建日志文件 touch /data/mysql/log/mysqld.log # 修改權(quán)限 chown -R mysql.mysql /applications/mysql chown -R mysql.mysql /data/mysql
MySQL配置文件
[client] port=3306 socket=/data/mysql/mysql.sock [mysqld] port=3306 datadir=/data/mysql/data basedir=/applications/mysql pid-file=/data/mysql/mysqld.pid socket=/data/mysql/mysql.sock user=mysql character-set-server=utf8mb4 default-storage-engine=INNODB collation-server = utf8mb4_general_ci init_connect='SET NAMES utf8mb4' max_connections = 1000 max_connect_errors = 1200 max_allowed_packet = 128M explicit_defaults_for_timestamp = true query_cache_size = 0 query_cache_type = 0 log_error = /data/mysql/log/error.log slow_query_log = 1 slow_query_log_file = /data/mysql/log/slow.log log_queries_not_using_indexes = 1 log_throttle_queries_not_using_indexes = 5 long_query_time = 8 log_slow_slave_statements = 1 min_examined_row_limit = 100 expire_logs_days = 5 tmpdir = /tmp innodb_buffer_pool_size = 128M [mysqld_safe] log-error=/data/mysql/log/mysqld.log pid-file=/data/mysql/mysqld.pid
# 同步數(shù)據(jù) /applications/mysql/bin/mysql_install_db --basedir=/applications/mysql/ --datadir=/data/mysql/data/ --user=mysql
配置并啟動(dòng)
cp /applications/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld vim /etc/init.d/mysqld
# 修改以下兩行 basedir=/applications/mysql datadir=/data/mysql/data
# 查看是否啟動(dòng) netstat -tunlap | grep mysql # 添加服務(wù)并設(shè)置為開機(jī)自啟動(dòng) chkconfig --add mysqld chkconfig mysqld on
初始化數(shù)據(jù)庫
/applications/mysql/bin/mysql_secure_installation
-- 設(shè)置用戶密碼 alter user 'root'@'localhost' identified by '123456'; -- 允許root遠(yuǎn)程訪問 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; FLUSH PRIVILEGES;
Django
配置pip3源
mkdir /root/.pip touch /root/.pip/pip.conf echo '[global]' >> /root/.pip/pip.conf echo 'trusted-host=mirrors.aliyun.com' >> /root/.pip/pip.conf echo 'index-url=https://mirrors.aliyun.com/pypi/simple/' >> /root/.pip/pip.conf
創(chuàng)建虛擬環(huán)境安裝依賴
# PublisherPro,一個(gè)支持MD輕量級(jí)的CMS程式. git clone https://gitee.com/bluemiaomiao/PublisherPro.git pip3 install virtualenv cd PROJECT_DIR virtualenv venv source venv/bin/activate pip3 install -r requestments.txt pip3 install uwsgi mkdir log mkdir script touch PublisherPro/script/uwsgi.pid touch PublisherPro/script/uwsgi.status vim uwsgi.ini
修改項(xiàng)目配置
# PROJECT_DIR/PROJECT_NAME/settings.py # 設(shè)置為生產(chǎn)環(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)文件相關(guān) # 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
配置文件內(nèi)容
# uwsig使用配置文件啟動(dòng) [uwsgi] # 項(xiàng)目目錄 chdir=/applications/website/PublisherPro # 指定項(xiàng)目的application module=PublisherPro.wsgi:application # 指定sock的文件路徑 socket=/applications/website/PublisherPro/script/uwsgi.sock # 進(jìn)程個(gè)數(shù) workers=5 pidfile=/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 # 啟動(dòng)uwsgi的用戶名和用戶組 uid=pubpro gid=pubpro # 啟用主進(jìn)程 master=true # 自動(dòng)移除unix Socket和pid文件當(dāng)服務(wù)停止的時(shí)候 vacuum=true # 序列化接受的內(nèi)容,如果可能的話 thunder-lock=true # 啟用線程 enable-threads=true # 設(shè)置自中斷時(shí)間 harakiri=30 # 設(shè)置緩沖 post-buffering=4096 # 設(shè)置日志目錄 daemonize=/applications/website/PublisherPro/log/uwsgi.log
創(chuàng)建用戶和組并修改權(quán)限
# 創(chuàng)建用戶 useradd pubpro -s /sbin/nologin -M # 檢查結(jié)果 id pubpro # 修改權(quán)限 chown -R pubpro.pubpro /applications/website/PublisherPro/ # 檢查結(jié)果 ll -d /applications/website/PublisherPro/
測試Django應(yīng)用
# 啟動(dòng)應(yīng)用 uwsgi --ini uwsgi.ini # 重載應(yīng)用 uwsgi --reload script/uwsgi.pid # 狀態(tài)信息 uwsgi --connect-and-read script/uwsgi.status # 停止應(yīng)用 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; # 指定項(xiàng)目路徑uwsgi location / { # 導(dǎo)入一個(gè)Nginx模塊他是用來和uWSGI進(jìn)行通訊的 include uwsgi_params; # 設(shè)置連接uWSGI超時(shí)時(shí)間 uwsgi_connect_timeout 30; # 指定uwsgi的sock文件所有動(dòng)態(tài)請(qǐng)求就會(huì)直接丟給他 uwsgi_pass unix:/data/PublisherPro/script/uwsgi.sock; } # 指定靜態(tài)文件路徑 location /static/ { alias /data/PublisherPro/static; index index.html index.htm; } }
以上是“Django+uwsgi+Nginx上線的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。