基於Ubuntu18.04一站式部署

Python3.6.8的安裝

1. 安裝依賴

~$ sudo apt install openssl* zlib*

2. 安裝python3.6.8(個人建議從官網下載後上傳到伺服器)

點選下載python3.6.8官網原始碼 或根據下面的命令安裝

~$ sudo wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

3. 解壓.tgz檔案 並 進入Python-3.6.8資料夾

~$ sudo tar -xzvf Python-3.6.8.tgz

~$ cd Python-3.6.8

4. 將python安裝到目錄下

~$ sudo ./configure --prefix=/usr/local/python --with-ssl --enable-optimizations

5. 編譯並且安裝

~$ sudo make && sudo make install

6. 安裝完成之後 可以刪除原始碼壓縮包與解壓出來的檔案夾了

~$ rm -rf Python-3.6.8.tgz Python-3.6.8

7. 建立python3軟連結

~$ sudo ln -s /usr/local/python/bin/python3.6 /usr/bin/python
# 注意 報錯資訊:
ln: failed to create symbolic link '/usr/bin/python': File exists

7.1 解決方法與檢視指向

1. 檢視Linux軟連線存放位置
~$ cd /usr/bin
2. 檢視現在的python軟連線指向的版本
~$ ls -al python
3. 解決方法:
~$ sudo ln -sf /usr/python/bin/python3 /usr/bin/python

8. 建立pip3的軟連結

sudo ln -s /usr/local/python/bin/pip3.6 /usr/bin/pip

驗證安裝是否成功 輸入python3 回車即可

~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
~$

虛擬環境的安裝

1. 根據上述python的安裝 已經實現安裝pip3 安裝依賴

# 檢視pip3 是否成功執行
~$ pip3 list # 安裝依賴
~$ pip3 install virtualenv
~$ pip3 install virtualenvwrapper

2. 配置檔案的修改

# 檢視配置檔案是否存在 .bashrc
~$ ls -lah .bashrc # 在檔案最後面新增 如果沒有許可權 請使用sudo vim ~/.bashrc
~$ vim ~/.bashrc export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
source ~/.local/bin/virtualenvwrapper.sh # 退出編輯 並儲存
先按ESC鍵,再輸入:wq # 儲存成功後 重啟配置檔案 (注意 需要重啟配置檔案 才會生效)
~$ source ~/.bashrc

3. 虛擬環境的操作(增 刪 查 退出)

#1. 建立虛擬環境
~$ mkvirtualenv xxx #2. 刪除虛擬環境
~$ rmvirtualenv xxx #3. 檢視虛擬環境
~$ workon # 之所以能用workon 是因為剛剛的.bashrc檔案裡面配置了 #4. 退出虛擬環境(在使用的時候)
~$ deactivate

mysql的安裝與配置

1. MySQL的安裝命令

~$ sudo apt-get install mysql-server

2. 安裝完成後 測試連線

# 因為安裝的過程中沒讓設定密碼,進不去mysql。
~$ mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

3. 修改配置檔案(mysqld.cnf) 每次修改配置檔案都重啟

# /etc/mysql/mysql.conf.d/mysqld.cnf MySQL的配置檔案
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf # 在檔案最後面 新增一句 不啟動grant-tables授權表 作用可以不用密碼登入進去mysql。
skip-grant-tables # 退出編輯 並儲存
先按ESC鍵,再輸入:wq

4. 重啟MySQL服務 需要服務登入密碼驗證

~$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: ubuntu,,, (ubuntu)
Password:

5. 設定密碼 並重啟服務

# 此時是可以不用密碼登入mysql的
~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.35-0ubuntu0.18.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; # 使用某庫
Reading table ...
Database changed # 成功的提示 mysql> update user set authentication_string=password("pwd") where user="root"; # 修改root使用者的密碼
Query OK ... # 成功的提示 mysql> flush privileges; # 重新整理許可權
Query OK ... # 成功的提示 mysql> exit # 退出mysql # 將剛剛的配置檔案裡面的skip-grant-tables註釋掉
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# skip-grant-tables # 重啟MySQL服務 以確保修改的配置檔案生效
~$ service mysql restart

6. 如果此時登入mysql還是失敗(下面有些命令可在上述中查到)

# 此時如果登入mysql還是失敗的話 請執行以下步驟
~$ mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) #1. 將配置檔案的skip-grant-tables註釋開啟 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#2. 重啟MySQL服務 service mysql restart
#3. 此時是不需要密碼登入mysql的 mysql -uroot -p
#4. use mysql; #5. select user, plugin from user; # 檢視user表的user,plugin欄位
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+ #6. root對應的plugin為auth_socket 這也是登入不了的原因 修改plugin值
mysql> update user set authentication_string=password("pwd"),plugin='mysql_native_password' where user='root'; #7. 再次輸入檢視user表
mysql> select user,plugin from user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+ #8. 修改成功 退出mysql exit
#9. 修改配置檔案 將skip-grant-tables註釋 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#10. 重啟MySQL服務 service mysql restart

7. 連線mysql成功 並設定外部可連線(注意安全組開放3306埠)

#1. 此時登入mysql 就要使用設定的密碼登入了
~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '×××' WITH GRANT OPTION; # 命令中的 % 代表所有ip地址 xxx:你的密碼 mysql> flush privileges; # 重新整理許可權 mysql> exit # 退出mysql #2. 找到配置檔案 將bing註釋掉
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# bind=127.0.0.1 註釋掉

redis的安裝與配置

1. 安裝redis

~$ sudo apt-get install redis-server

2. 檢視redis的程序

~$ ps -ef|grep redis

3. 修改配置檔案(需要root許可權)

~$ sudo vim /etc/redis/redis.conf

4. 在bind下新增密碼設定(並註釋bind)

# bind 127.0.0.1 ::1 # 註釋bind 目的為了外部可連線
requirepass pwd # 設定密碼 外部連線需要通過密碼連線

5. redis的啟動/重啟

~$ service redis start/restart

6. 檢視埠(6379)

~$ netstat -nlt|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp6 0 0 :::6379 :::* LISTEN

nginx的安裝與配置

1. 安裝nginx

~$ sudo apt-get install nginx

2. 檢查安裝是否成功

~$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

3. nginx的啟動/重啟

~$ service nginx start/restart

4. 檢視程序

~$ ps -ef|grep nginx

5. 檢視nginx的相關檔案儲存位置

~$ whereis nginx

6. 相關檔案的介紹

/usr/sbin/nginx:主程式
/usr/lib/nginx: 模組依賴庫檔案
/etc/nginx:存放配置檔案
/usr/share/nginx:存放靜態檔案
/var/log/nginx:存放日誌