上一篇: [原創: 云服務器實戰系列2] 禁用root以及通過遠程接入
注意: 安裝的系統是Centos 7 x86/64位
基礎軟件包含:
- python3
- nginx
- git
- docker
- mysql
- redis
- supervisor(守護進程)
- etc...
python3
centos系統初始化時會安裝有python2.6.x版本, 此處我們使用python3
注意: 如果本機安裝了python2,盡量不要管它,使用python3運行python腳本就好,因為可能有程序依賴目前的python2環境,
1. 安裝依賴
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gcc
2. 下載python3
最新的tgz安裝包請參考官網 https://www.python.org/downloads/
sudo wget http://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
3. 安裝python3
個人習慣安裝在/usr/local/python3
創建目錄
sudo mkdir -p /usr/local/python3
解壓下載好的Python-3.x.x.tgz包
sudo tar -zxvf Python-3.7.5.tgz
4. 編譯安裝。
進入解壓后的目錄 && 編譯安裝
cd Python-3.7.5.tgz
sudo ./configure --prefix=/usr/local/python3
sudo make && make install
5.建立python3的軟鏈
[account@yunServer Python-3.7.5]$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[account@yunServer Python-3.7.5]$ python3
Python 3.7.5 (default, Nov 5 2019, 23:53:13)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
6. 配置環境變量
vi ~/.bash_profile
修改倒數第二行, 將/usr/local/python3/bin加入PATH
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
按ESC,輸入:wq回車退出。
修改完記得執行行下面的命令,讓上一步的修改生效:
source ~/.bash_profile
檢查Python3及pip3是否正常可用:
[account@yunServer Python-3.7.5]$ python3 -V
Python 3.7.5
[account@yunServer Python-3.7.5]$ pip3 -V
pip 19.2.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
python3 python2共存
刪除原先的python2的軟鏈接
rm -r /usr/bin/python
新建python軟鏈接指向python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python
因為執行yum需要python2版本,所以我們還要修改yum的配置,執行:
vi /usr/bin/yum
把#! /usr/bin/python修改為#! /usr/bin/python2, 如下圖所示:
同理:
vi /usr/libexec/urlgrabber-ext-down
將文件里面的#! /usr/bin/python 也要修改為#! /usr/bin/python2, 如下圖所示
nginx
安裝依賴
在編譯安裝之前,需要安裝nginx依賴包pcre-devel
sudo yum -y install pcre-devel
新建安裝目錄
這里,將會把nginx安裝到/usr/local/下, 故在此下新建目錄:
sudo mkdir /usr/local/nginx
下載nginx源碼包
在nginx官網尋找合適的nginx源碼包,通過wget下載,這里下載的是1.15.0版本
sudo wget http://nginx.org/download/nginx-1.15.0.tar.gz
下載完成之后,解壓源碼包
sudo tar -zxvf nginx-1.15.0.tar.gz
進入剛解壓出來的目錄
cd nginx-1.15.0
編譯
sudo ./configure --prefix=/usr/local/nginx
安裝
sudo make && make install
配置nginx開機啟動
配置nginx開機啟動,切換到/lib/systemd/system目錄,創建nginx.service文件
cd /lib/systemd/system
sudo vim nginx.service
添加以下內容
并添加以下內容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存并退出,使用下面命令設置開機啟動:
systemctl enable nginx.service
相關命令
- systemctl start nginx.service, 啟動,也可以使用sbin/nginx啟動
- systemctl stop nginx.service, 結束nginx
- systemctl restart nginx.service, 重啟
- systemctl statis nginx.service, 查看運行狀態
配置文件
/usr/local/nginx/conf/nginx.conf文件是nginx默認的配置文件,對其修改即可。
驗證
配置好之后, 啟動nginx: systemctl start nginx.service
.
通過瀏覽器直接訪問外網IP, 發現無法連接?
查詢資料, 原來阿里云默認有一個安全組, 在控制端口的出入.
在阿里云實例的控制界面中, 找到 "更多" -> "網絡和安全組" -> "安全組配置", 如下圖所示
之后進入到:
選擇"配置規則"
同時在"入方向"和"出方向"添加這個端口的安全規則即可, 這里我的配置是:
配置好規則之, 即可在瀏覽器通過訪問外網IP或與外網IP綁定的域名了
git
名詞簡介:
- git, 是一種版本控制系統
- gitlib, 是用于實現git功能的開發庫
- github, 是一個基于git實現的在線代碼托管倉庫
- gitlab, 是一個基于git實現的在線代碼倉庫托管軟件,你可以用gitlab自己搭建一個類似于github一樣的系統
安裝git
sudo yum -y install git
驗證
[account@yunServer ~]$ git --version
git version 1.7.1
docker
安裝依賴
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
設置穩定的docker repo庫
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安裝 Docker Engine-Community
安裝最新版本的 Docker Engine-Community 和 containerd
sudo yum install docker-ce docker-ce-cli containerd.io
啟動docker
sudo systemctl start docker
驗證docker
通過運行 hello-world 映像來驗證是否正確安裝了 Docker Engine-Community 。
sudo docker run hello-world
[account@yunServer Python-3.7.5]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
mysql
docker pull mysql
查找Docker Hub上的mysql鏡像
[account@yunServer Python-3.7.5]$ sudo docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation?? 8785 [OK]
mariadb MariaDB is a community-developed fork of MyS?? 3079 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create?? 652 [OK]
percona Percona Server is a fork of the MySQL relati?? 459 [OK] ...
這里我們拉取官方的鏡像,標簽為5.6
[account@yunServer ~]$ sudo docker pull mysql:5.6
等待下載完成后,我們就可以在本地鏡像列表里查到REPOSITORY為mysql,標簽為5.6的鏡像。
[account@yunServer ~]$ sudo docker images |grep mysql
mysql 5.6 b3983abaa3fe 3 weeks ago 302MB
啟動docker mysql
在/usr/local/mysql
目錄下新建data
目錄.用于接將宿主機文件系統上的文件路徑映射到容器中,兩邊雙向同步. 然后執行:
sudo docker run --restart=always --name my_mysql -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=youpassword -p 3306:3306 -d mysql:5.6
命令說明:
- --restart=always, --restart參數有3個可選值 :
- no, 默認值, 表示容器退出時,docker不自動重啟容器
- on-failure, 若容器的退出狀態非0,則docker自動重啟容器,還可以指定重啟次數,若超過指定次數未能啟動容器則放棄, 例如:
docker update --restart=on-failure:3 [容器名]
- always, 只要容器退出,則docker將自動重啟容器, 如果容器啟動時沒有設置--restart參數,則通過命令
docker update --restart=always [容器名]
進行更新.
- --name my_mysql, 將運行的mysql容器命名為my_mysql
- -v /usr/local/mysql/data:/var/lib/mysql, 將主機中/usr/local/mysql目錄下的data掛載到容器的/var/lib/mysql
- -d mysql:5.6, 后臺運行mysql5.6
- MYSQL_ROOT_PASSWORD=youpassword, 啟動時設置密碼為: "youpassword"
- -p 3306:3306, 將容器的3306端口映射到主機3306端口
若想把目前為止對my_mysql容器做的變更保存下來,可以使用docker commit指令:
docker commit my_mysql local_mysql:5.6
此時, 查看本地容器鏡像, 會發現多出一個:
[account@yunServer test]# docker commit my_mysql local_mysql:5.6
sha256:dde65afa24be96e5f5214c2bdd6acd92c8851df7ab9cdd6d3ec824e798eef70a
[account@yunServer test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
local_mysql 5.6 dde65afa24be 6 seconds ago 302MB
mysql 5.6 b3983abaa3fe 3 weeks ago 302MB
hello-world latest fce289e99eb9 10 months ago 1.84kB
redis 3.2 87856cc39862 12 months ago 76MB
[account@yunServer test]#
此時, local_mysql鏡像就保存了當前對my_mysql的更改, 其他云服務器若想使用, 即可使用該鏡像.
驗證
[account@yunServer test]# docker exec -it my_mysql /bin/bash
root@4ffaa2f14bf8:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.46 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user, host from user;
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
mysql>
本地遠程連接云服務器docker mysql
默認情況下, 阿里云服務器并沒有開放3306端口, 需要添加安全組規則, 與上文中的開放nginx 80一樣, 添加3306端口的安全組規則配置即可.
然后, 就可以通過Navicat for Mysql訪問云服務器中的mysql數據庫了.
redis
docker pull redis
這里我們拉取官方的鏡像,標簽為3.2
[account@yunServer ~]$ sudo docker pull redis:3.2
等待下載完成后,我們就可以在本地鏡像列表里查到REPOSITORY為redis,標簽為3.2的鏡像。
[account@yunServer ~]$ sudo docker images redis
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 3.2 87856cc39862 12 months ago 76MB
啟動docker redis
[account@yunServer local]$ sudo docker run --restart=always -p 6379:6379 --name redis -v /usr/local/redis/data:/data -d redis:3.2 redis-server --requirepass "password" --appendonly yes
8b26e70f738426982a3e17ca9d795563b462ed1027cfc073bc2b5f77dba00448
命令說明:
- --restart=always, --restart參數有3個可選值 :
- no, 默認值, 表示容器退出時,docker不自動重啟容器
- on-failure, 若容器的退出狀態非0,則docker自動重啟容器,還可以指定重啟次數,若超過指定次數未能啟動容器則放棄, 例如:
docker update --restart=on-failure:3 [容器名]
- always, 只要容器退出,則docker將自動重啟容器, 如果容器啟動時沒有設置--restart參數,則通過命令
docker update --restart=always [容器名]
進行更新.
- -p 6379:6379, 將容器的6379端口映射到主機的6379端口
- --name redis, 將運行的redis容器命名為"redis"
- -v /usr/local/redis/data:/data, 將主機中/usr/local/redis目錄下的data掛載到容器的/data
- -d redis:3.2 redis-server, 后臺運行redis:3.2 redis-server
- --requirepass "password", 啟動時設置密碼為: "password"
- redis-server --appendonly yes : 在容器執行redis-server啟動命令,并打開redis持久化配置
查看運行狀態
[account@yunServer local]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b26e70f7384 redis:3.2 "docker-entrypoint.s?? 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp redis
使用docker redis
注意, 我們啟動redis時設置了密碼, 因此在redis-cli中輸入命令時, 會要求輸入密碼認證, 此時我們可以輸入:
auth password
來通過認證.
[account@yunServer local]$ sudo docker exec -it 8b26e70f7384 redis-cli
[sudo] password for account:
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> auth password
OK
127.0.0.1:6379> info
# Server
redis_version:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b0df607ad3315254
redis_mode:standalone
os:Linux 3.10.0-957.21.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:6.3.0
process_id:1
...
supervisor
安裝
sudo pip install supervisor
生成默認配置
[root@yunServer etc]# cd /usr/local/python3/bin/
[root@yunServer bin]# pwd
/usr/local/python3/bin
[root@yunServer bin]# ./echo_supervisord_conf > /etc/supervisord.conf
添加開機自啟服務
新建文件:
vim /lib/systemd/system/supervisord.service
并添加以下內容:
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
添加軟鏈接:
ln -s /usr/local/python3/bin/supervisord /usr/bin/supervisord
將啟動腳本都能夠添加到systemctl自啟動服務
[root@yunServer bin]# systemctl enable supervisord.service
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.
啟動supervisord
[account@yunServer bin]# sudo systemctl start supervisord.service
查看運行狀態
[account@yunServer bin]# sudo systemctl status supervisord.service
?.supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2019-11-10 11:22:36 CST; 4s ago
Process: 2521 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
Main PID: 2524 (supervisord)
Tasks: 1
Memory: 14.7M
CGroup: /system.slice/supervisord.service
?..2524 /usr/local/python3/bin/python3.7 /usr/bin/supervisord -c /etc/supervisord.conf
Nov 10 11:22:36 yunServer systemd[1]: Starting Process Monitoring and Control Daemon...
Nov 10 11:22:36 yunServer systemd[1]: Started Process Monitoring and Control Daemon.
[account@yunServer bin]#
其他
centos7 相關命令
查看消耗內存最多的前40個進程:
ps auxw|head -1;ps auxw|sort -rn -k4|head -40
查看內存使用情況:
free -m
徹底停止進程
kill -9 PIDNumber
systemctl
- systemctl enable docker.service, 設置docker開機自啟動
- systemctl disable docker.service, 停止docker開機自啟動
- systemctl start docker.service, 啟動docker服務
- systemctl stop docker.service, 停止docker服務
- systemctl status docker.service, 查看docker服務運行狀態
- systemctl restart docker.service, 重新啟動docker服務
- systemctl list-units --type=service, 查看所有已啟動的服務
docker
- docker ps, 查看容器運行狀態
- docer cp
- 從容器中copy文件至宿主機中, 如:
- 將容器
my_mysql
中的/etc/mysql/my.cnf
復制至/usr/local/mysql/
目錄 - 命令為:
sudo docker cp my_mysql:/etc/mysql/my.cnf /usr/local/mysql/
,
- 將容器
- 從宿主機中將文件copy至容器中, 如:
docker cp /opt/test.js my_mysql:/etc/mysql/
- 從容器中copy文件至宿主機中, 如:
- docker container update, 修改Docker容器啟動配置參數, 有時候,我們創建容器時忘了添加參數 --restart=always ,當 Docker 重啟時,容器未能自動啟動, 可能通過:
- 停止容器,
docker stop my_mysql
- 更新參數,
docker container update --restart=always my_mysql
- 啟動容器,
docker start my_mysql
- 停止容器,
- docker commit, 基于當前的容器創建一個新的鏡像(會保存對當前容器的修改), 具體用法可以參考: docker commit
至此, 基礎軟件環境已安裝完成!