在centos7下從零搭建Nginx+uWSGI+Django

1、安裝Python3
  在centos7中系統自帶Python2.7,須要本身安裝Python3
  一、安裝依賴html

sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

  二、安裝wgetnode

sudo yum install wget

  三、獲取pythonpython

wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz

  四、解壓、編譯mysql

tar -xf Python-3.7.3.tar.xz # 解壓
./configure prefix=/usr/local/python3 # 配置安裝目錄
make # 編譯
make install # 安裝

五、添加軟鏈接linux

ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

 

2、安裝Djangonginx

sudo pip3 install django sudo ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin # 創建軟鏈接

  因爲pymysql沒法在django2.2上使用,也不是徹底沒辦法,但django推薦使用mysqlclient,參考https://docs.djangoproject.com/zh-hans/2.2/ref/databases/#mysql-notessql

  安裝mysqlclientdjango

sudo yum install mysql-devel sudo pip3 install mysqlclient

 

 

3、安裝MySQL
  一、確認系統是否裝有mysqlcentos

rpm -qa | grep mysql

  若是裝有:app

rpm -e mysql  // 普通刪除模式 rpm -e --nodeps mysql  // 強力刪除模式,若是使用上面命令刪除時,提示有依賴的其它文件,則用該命令能夠對其進行強力刪除

  二、下載yum資源包

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

  三、使用yum安裝mysql

sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm sudo yum install mysql-community-server chown mysql:mysql -R /var/lib/mysql # 修改mysql權限
sudo mysqld --initialize # 初始化
systemctl start mysqld # 啓動mysql服務
grep 'temporary password' /var/log/mysqld.log # 查看臨時密碼
mysql -uroot -p # 使用臨時密碼登陸

  四、修改密碼
    因爲默認安裝有validate_password,所以密碼要求:至少包含1個數字字符,1個小寫字符,1個大寫字符和1個特殊(非字母數字)字符,長度不小於8位;

ALTER USER ‘root’@'localhost' IDENTIFIED BY 'new_password';

    若是但願使用簡單的密碼,能夠修改validate_password的策略,validate_password.policy,有LOW,MEDIUM,STRONG三種等級,默認爲MEDIUM;
    參考:https://dev.mysql.com/doc/refman/8.0/en/validate-password.html

SET GLOBAL validate_password.policy=0; # 修改成LOW,可是要求長度不小於8位 SET GLOBAL validate_password.length=4; # 修改長度

 


4、安裝uWSGI
  一、安裝uWSGI

pip3 install uwsgi ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi # 創建軟鏈接

  二、啓動
    a、使用命令行啓動
      1.進入項目目錄
      2.

uwsgi --http 192.168.57.129:8000 --file mysite/wsgi.py --static-map=/static=static
--http 
--file
--processes 選項添加更多的進程
--threads 選項添加更多的線程
--master 一個master進程 (在Inc死掉的時候會生成它們),後面沒有參數
--stats 127.0.0.1:9191 容許將uWSGI的內部統計數據做爲JSON導出

    b、使用配置文件啓動
      1.創建yourfile.ini文件

[uwsgi] #http = 0.0.0.0:3031 # 當只使用uWSGI時,使用http,配合nginx,使用socket與nginx創建鏈接
chdir = /root/mysite/ module = mysite.wsgi:application wsgi-file = myproject/wsgi.py socket = /root/mysite/script/uwsgi.sock pidfile = /root/mysite/script/uwsgi.pid uid = root gid = root master = true processes = 4 threads = 2 stats = 127.0.0.1:9191 static-map=/static=static

      2.啓動

uwsgi --ini youfile.ini

 

 

5、安裝nginx
  參考官方文檔:https://nginx.org/en/linux_packages.html#RHEL-CentOS
  一、Install the prerequisites:

sudo yum install yum-utils

  二、To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key

  -三、不是必須,若是不裝標準版的nginx才須要注意。By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

sudo yum-config-manager --enable nginx-mainline

  四、To install nginx, run the following command:

sudo yum install nginx  # When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62, and if so, accept it.

  五、配置,在/etc/nginx/conf.d/創建配置文件my.conf,這只是一個簡單配置

server {   listen 80;   server_name 192.168.57.129;   charset utf-8;   #access_log logs/host.access.log main;
  location / {     include uwsgi_params;     uwsgi_pass unix:/root/mysite/script/uwsgi.sock;     uwsgi_connect_timeout 30;   }   #靜態文件,nginx本身處理,不去uWSGI請求
  location /static/ {     alias /root/mysite/static/;     index index.html index.htm;   }   error_page 404 /404.html;   error_page 500 502 503 504 /50x.html; }

  六、注意/etc/nginx/nginx.conf中的user,默認爲nginx,須要改成主機用戶
  七、啓動nginx

sudo nginx -c /etc/nginx/nginx.conf # 啓動
sudo nginx -s quit 或者 sudo nginx -s stop # 中止

 

  若是uwsgi_params文件缺失,本身添加,注意路徑要與配置文件中對應,默認應該在nginx目錄下有uwsgi_params文件。

uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
相關文章
相關標籤/搜索