[原创: 云服务器实战系列] Centos7 基础软件环境搭建

2019-11-10  本文已影响0人  HelloJames

上一篇: [原创: 云服务器实战系列2] 禁用root以及通过远程接入

注意: 安装的系统是Centos 7 x86/64位

基础软件包含:

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, 如下图所示:

image

同理:

vi /usr/libexec/urlgrabber-ext-down

将文件里面的#! /usr/bin/python 也要修改为#! /usr/bin/python2, 如下图所示

image

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

相关命令

配置文件

/usr/local/nginx/conf/nginx.conf文件是nginx默认的配置文件,对其修改即可。

验证

配置好之后, 启动nginx: systemctl start nginx.service.
通过浏览器直接访问外网IP, 发现无法连接?

image

查询资料, 原来阿里云默认有一个安全组, 在控制端口的出入.
在阿里云实例的控制界面中, 找到 "更多" -> "网络和安全组" -> "安全组配置", 如下图所示

image

之后进入到:

image

选择"配置规则"

image

同时在"入方向"和"出方向"添加这个端口的安全规则即可, 这里我的配置是:

image

配置好规则之, 即可在浏览器通过访问外网IP或与外网IP绑定的域名了

git

名词简介:

安装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

命令说明:

若想把目前为止对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数据库了.

image

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

命令说明:

查看运行状态

[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

docker

至此, 基础软件环境已安装完成!

上一篇下一篇

猜你喜欢

热点阅读