目錄html
# 購買阿里雲服務器 # 短時間或是測試使用,建立 按量收費 服務器,能夠隨時刪除,刪除後再也不計費,但要保證帳戶餘額100元以上
1)帳號 >: ssh root@39.100.107.176 2)密碼 >: ********
1)如下全部的服務器命令都可以在管理員權限下執行 >: sudo 命令
1)編輯配置文件 >: vim ~/.bash_profile 2)將原來內容所有刪除掉 >: ggdG 3)進入編輯狀態:填入下方兩行 >: i export PATH=$PATH:$HOME/bin PS1='Path:\w\n>:' 4)退出編輯狀態 >: esc 5)保存修改並退出 >: :wq 6)生效配置 >: source ~/.bash_profile
>: yum update -y
>: yum -y groupinstall "Development tools" >: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
1)前往用戶根目錄 >: cd ~ 2)下載mysql57 >: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm scp -r C:\Users\dell\Desktop\pkg\mysql57-community-release-el7-10.noarch.rpm root@39.100.107.176:~ 3)安裝mysql57 >: yum -y install mysql57-community-release-el7-10.noarch.rpm >: yum -y install mysql-community-server 4)啓動mysql57並查看啓動狀態 >: systemctl start mysqld.service >: systemctl status mysqld.service 5)查看默認密碼並登陸 >: grep "password" /var/log/mysqld.log >: mysql -uroot -p 6)修改密碼 >: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; >: ALTER USER 'root'@'localhost' IDENTIFIED BY 'Owen1234?';
1)前往用戶根目錄 >: cd ~ 2)下載redis-5.0.5 >: wget http://download.redis.io/releases/redis-5.0.5.tar.gz >: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz root@39.100.107.176:~ 3)解壓安裝包 >: tar -xf redis-5.0.5.tar.gz 4)進入目標文件 >: cd redis-5.0.5 5)編譯環境 >: make 6)複製環境到指定路徑完成安裝 >: cp -r ~/redis-5.0.5 /usr/local/redis 7)配置redis能夠後臺啓動:修改下方內容 >: vim /usr/local/redis/redis.conf daemonize yes 8)完成配置修改 >: esc >: :wq 9)創建軟鏈接 >: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server >: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli 10)後臺運行redis >: redis-server & ctrl + c 11)測試redis環境 >: redis-cli ctrl + c 12)關閉redis服務 >: pkill -f redis -9
1)前往用戶根目錄 >: cd ~ 2)下載 或 上傳 Python3.6.7 >: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz >: scp -r 本地Python-3.6.7.tar.xz ssh root@39.100.233.226:服務器路徑 >: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@39.100.233.226:~ 3)解壓安裝包 >: tar -xf Python-3.6.7.tar.xz 4)進入目標文件 >: cd Python-3.6.7 5)配置安裝路徑:/usr/local/python3 >: ./configure --prefix=/usr/local/python3 6)編譯並安裝 >: make && sudo make install 7)創建軟鏈接:終端命令 python3,pip3 >: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3 >: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3 8)刪除安裝包與文件: >: rm -rf Python-3.6.7 >: rm -rf Python-3.6.7.tar.xz
1)建立pip配置路徑 >: mkdir ~/.pip 2)進入目錄編輯配置文件:填入下方內容 cd ~/.pip && vim pip.conf [global] index-url = http://pypi.douban.com/simple [install] use-mirrors =true mirrors =http://pypi.douban.com/simple/ trusted-host =pypi.douban.com
1)在真實環境下安裝 pip3 install uwsgi 2)創建軟鏈接 ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
1)安裝依賴 >: pip3 install virtualenv >: pip3 install virtualenvwrapper 2)創建虛擬環境軟鏈接 >: ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv 3)配置虛擬環境:填入下方內容 >: vim ~/.bash_profile VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source /usr/local/python3/bin/virtualenvwrapper.sh 4)退出編輯狀態 >: esc 5)保存修改並退出 >: :wq 6)更新配置文件內容 >: source ~/.bash_profile 7)虛擬環境默認根目錄:~/.virtualenvs
1)建立虛擬環境 >: mkvirtualenv test_venv 2)安裝依賴 >: pip install django 3)前往目標目錄,建立項目工做目錄,再進入 >: cd /home >: mkdir project >: cd project 4)建立Django項目,並進入 >: django-admin startproject test_site >: cd test_site 5)完成項目配置:修改下方几行內容 >: vim /home/project/test_site/test_site/settings.py ALLOWED_HOSTS = ['*'] #DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } #} 6)跑原生服務 >: python3 manage.py runserver 0.0.0.0:80
1)前往用戶根目錄 >: cd ~ 2)下載nginx1.13.7 >: wget http://nginx.org/download/nginx-1.13.7.tar.gz 3)解壓安裝包 >: tar -xf nginx-1.13.7.tar.gz 4)進入目標文件 >: cd nginx-1.13.7 5)配置安裝路徑:/usr/local/nginx >: ./configure --prefix=/usr/local/nginx 6)編譯並安裝 >: make && sudo make install 7)創建軟鏈接:終端命令 nginx >: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx 8)刪除安裝包與文件: >: rm -rf nginx-1.13.7 >: rm -rf nginx-1.13.7.tar.xz 9)測試Nginx環境,服務器運行nginx,本地訪問服務器ip >: nginx >: 服務器綁定的域名 或 ip:80
1)啓動 >: nginx 2)關閉nginx >: nginx -s stop 3)重啓nginx >: nginx -s reload 4)查看端口,強行關閉 >: ps -aux|grep nginx >: kill <pid:進程編號>
1)在項目的虛擬環境安裝uwsgi >: workon test_venv >: pip install uwsgi 2)項目根目錄配置uwsgi:填入下方內容 >: vim /home/project/test_site/test_site.xml <uwsgi> <socket>127.0.0.1:8808</socket> <!-- 內部端口,自定義 --> <chdir>/home/project/test_site/</chdir> <!-- 項目路徑 --> <module>test_site.wsgi</module> <!-- test_site爲wsgi.py所在目錄名--> <processes>4</processes> <!-- 進程數 --> <daemonize>uwsgi.log</daemonize> <!-- 日誌文件 --> </uwsgi> 3)完成項目配置:修改下方几行內容 >: vim /home/project/test_site/test_site/settings.py DEBUG = False ALLOWED_HOSTS = ['*'] 4)去向Nginx配置目錄,備份配置,徹底更新配置:填入下方內容 >: cd /usr/local/nginx/conf >: cp nginx.conf nginx.conf.bak >: vim nginx.conf >: ggdG >: i events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 8000; server_name 127.0.0.1; # 改成本身的域名,沒域名修改成127.0.0.1:80 charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi裏配置的同樣 uwsgi_param UWSGI_SCRIPT test_site.wsgi; #wsgi.py所在的目錄名+.wsgi uwsgi_param UWSGI_CHDIR /home/project/test_site/; # 項目路徑 } } } 5)啓動uwsgi >: uwsgi -x /home/project/test_site/test_site.xml 6)啓動nginx >: nginx 7)瀏覽器測試:http://39.100.152.198/admin 8)關閉uwsgi全部進程 >: pkill -f uwsgi -9
base_url: 'http://39.100.107.176:8000', // 設置公網ip
1)本地項目打包,前往luffycity項目目錄下 >: cnpm run build 2)上傳 >: scp -r dist root@39.100.107.176:~ 3)移動並重命名 mv ~/dist /home/html 4)去向Nginx配置目錄,備份配置,徹底更新配置:填入下方內容 >: cd /usr/local/nginx/conf >: cp nginx.conf nginx.conf.bak >: vim nginx.conf >: ggdG >: i events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name 127.0.0.1; # 改成本身的域名,沒域名修改成127.0.0.1:80 charset utf-8; location / { root /home/html; # html訪問路徑 index index.html; # html文件名稱 try_files $uri $uri/ /index.html; # 解決單頁面應用刷新404問題 } } }
prod.py:上線的配置文件,內容拷貝dev.py,前身就是settings.py前端
1)須要作上線修改的內容 DEBUG = False ALLOWED_HOSTS = [ '39.100.107.176' # 公網ip地址 ] CORS_ORIGIN_ALLOW_ALL = True # 容許全部跨域 CORS_ORIGIN_WHITELIST = [ ]
wsgi.py 和 manage.pyvue
1)須要作上線修改的內容 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod')
1)在項目的虛擬環境安裝uwsgi >: mkvirtualenv luffy >: workon luffy # 走下方 pip導入導出依賴 說明,將本地的環境依賴同步到服務器環境中 >: pip install uwsgi 2)項目根目錄配置uwsgi:填入下方內容 >: mkdir /home/project # 注:將後臺項目移至到/home/project,能夠上傳,也能夠git,項目設置公開(私密須要配置ssl) >: cd /home/project && git clone https://gitee.com/doctor_owen/luffyapi.git >: vim /home/project/luffyapi/luffyapi.xml <uwsgi> <socket>127.0.0.1:8808</socket> <!-- 內部端口,自定義 --> <chdir>/home/project/luffyapi/</chdir> <!-- 項目路徑 --> <module>luffyapi.wsgi</module> <!-- luffyapi爲wsgi.py所在目錄名--> <processes>4</processes> <!-- 進程數 --> <daemonize>uwsgi.log</daemonize> <!-- 日誌文件 --> </uwsgi> #### 3)配置上線項目的settings:見後臺項目部署準備視頻 4)去向Nginx配置目錄,備份配置,徹底更新配置:填入下方內容 >: vim /usr/local/nginx/conf/nginx.conf 5)在原來基礎上添加一個server events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 8000; server_name 127.0.0.1; # 改成本身的域名,沒域名修改成127.0.0.1:80 charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi裏配置的同樣 uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目錄名+.wsgi uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 項目路徑 } } } 見下方配置:pip環境 + 數據庫設置 + django2.0源碼 5)啓動uwsgi >: uwsgi -x /home/project/luffyapi/luffyapi.xml 6)啓動nginx >: nginx -s stop >: nginx >: nginx -s reload 7)瀏覽器測試:http://39.100.107.176:8000/xadmin 8)關閉uwsgi全部進程 >: pkill -f uwsgi -9
1) 本地導出項目環境,上傳線上,導入到線上環境中 本地操做 # 桌面新建env文件夾,開啓終端進入文件夾,執行下方命名 >: cd Desktop\env >: pip3 freeze > packages.txt # 注:把xadmin刪掉 >: scp -r packages.txt root@39.100.107.176:~ 服務器操做 # 進入虛擬環境 >: workon luffy # 導入 >: pip3 install -r packages.txt # 安裝xadmin >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2
1.管理員鏈接數據庫 >: mysql -uroot -pOwen1234? 2.建立數據庫 >: create database luffy default charset=utf8; # 3.設置權限帳號密碼 # 擁有公網或局域網,其餘主機連mysql >: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?'; # 要是本機連mysql連不上,再增長localhost域,本機就能夠登陸了 >: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?'; # 設置完有權限限制的帳號後必定要刷新權限 >: flush privileges; 4.退出mysql quit 5.修改 prod.py | manage.py >: vim /home/project/luffyapi/luffyapi/settings/prod.py "PASSWORD": "Luffy123?" >: vim /home/project/luffyapi/manage.py os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod') 6.源碼修改 >: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py # 36,37行註釋 >: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/operations.py # 146行添加 query = query.encode() 7.數據庫遷移 >: cd /home/project/luffyapi/ >: python manage.py makemigrations >: python manage.py migrate 8.建立超級用戶 >: python manage.py createsuperuser # 帳號密碼:admin|admin
# >: vim /home/project/luffyapi/luffyapi/settings/prod.py # 在STATIC_URL下方再添加兩句 STATIC_URL = '/static/' STATIC_ROOT = '/home/project/luffyapi/luffyapi/static' # 服務器的絕對路徑 STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),) # >: esc # >: :wq
可能錯誤 >: mkdir /home/project/luffyapi/luffyapi/static >: python /home/project/luffyapi/manage.py collectstatic
>: vim /usr/local/nginx/conf/nginx.conf events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 8000; server_name 127.0.0.1; # 改成本身的域名,沒域名修改成127.0.0.1:80 charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi裏配置的同樣 uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目錄名+.wsgi uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 項目路徑 } # 新增的配置靜態文件 location /static { alias /home/project/luffyapi/luffyapi/static; } } server { listen 80; server_name 127.0.0.1; # 改成本身的域名,沒域名修改成127.0.0.1:80 charset utf-8; location / { root /home/html; # html訪問路徑 index index.html; # html文件名稱 try_files $uri $uri/ /index.html; # 解決單頁面應用刷新404問題 } } } >: esc >: :wq
>: pkill -f uwsgi -9 >: uwsgi -x /home/project/luffyapi/luffyapi.xml >: nginx -s reload
# 一、真實環境和虛擬環境都要安裝uwsgi,將真實環境下的uwsgi創建軟鏈接 # 二、redis服務必定要後臺啓動:redis-server & # 三、uwsgi啓動django項目必定要進入虛擬環境下,由於環境都是安裝在虛擬環境中 # 四、服務器的日誌都會被記錄在於uwsgi配置文件 luffyapi.xml 同類目下的 uwsgi.log 中
>: mysql -uluffy -pLuffy123? >: use luffy
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教學總監', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 國內知名PYTHON語言推廣者 51CTO學院2016\2017年度最受學員喜好10大講師之一 多款開源軟件做者 曾任職公安部、飛信、中金公司、NOKIA中國研究院、華爾街英語、ADVENT、汽車之家等公司'); INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美團前端項目組架構師', NULL, 'teacher/mjj_icon.png', '是馬JJ老師, 一個集美貌與才華於一身的男人,搞過幾年IOS,又轉了前端開發幾年,曾就任於美團網任高級前端開發,後來由於不一樣意王興(美團老闆)的戰略佈局而出家作老師去了,有豐富的教學經驗,開起車來也絕不含糊。一直專一在前端的前沿技術領域。同時,愛好抽菸、喝酒、燙頭(錫紙燙)。 個人最愛是前端,由於前端妹子多。'); INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux學科帶頭人', NULL, 'teacher/lyy_icon.png', 'Linux運維技術專家,老男孩Linux金牌講師,講課風趣幽默、深刻淺出、聲音洪亮到爆炸'); INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python'); INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux'); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python開發21天入門', 'courses/alex_python.png', 0, 'Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python項目實戰', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2); INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系統基礎5周入門精講', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '計算機原理', '', '2019-07-14', 1); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '環境搭建', '', '2019-07-14', 1); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '項目建立', '', '2019-07-14', 2); INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux環境建立', '', '2019-07-14', 3); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '計算機原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '計算機原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '環境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '環境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web項目的建立', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3); INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的環境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);