虚拟机&linux基础开发环境配置

1.新建虚拟机

  • 新建虚拟机
  • 设置磁盘位置/大小
  • 设置cpu
  • 设置内存
  • 设置网络

2.安装系统

  • 选择光盘位置-安装系统-设置root账号密码

3.设置网络

  • 虚拟机设置网络为桥接,自动获取
  • 进入虚拟机并配置:
1
2
3
4
5
6
7
8
9
#查看网络名
ifconfig 或者 ip addr
#编辑网络配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens33
[修改内容]
ONBOOT=yes

#使配置生效
systemctl restart network
  • 测试外网链接
1
curl www.baidu.com
  • 如果没有网络,设置静态地址
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
vi /etc/sysconfig/network-scripts/ifcfg-ens33
[修改内容]
BOOTPROTO=static
IPADDR=192.168.20.72
NETMASK=255.255.255.0
GATEWAY=192.168.20.254
DNS1=8.8.8.8
DNS2=114.114.114.114

#使配置生效
systemctl restart network
  • 多网络设置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#复制网络配置文件
cp /etc/sysconfig/network-scripts/ifcfg-ens0 /etc/sysconfig/network-scripts/ifcfg-ens1
#编辑新增的配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens1
[修改内容]
NAME=eth1
DEVICE=eth1
ONBOOT=no

#使配置生效
systemctl restart network

4.linux基础配置

  • 添加yum软件源

    1
    2
    3
    4
    5
    6
    7
    8
    
    cd /etc/yum.repos.d/
    mkdir backupdy
    mv C* backupdy
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    或者
    yum -y install yum-utils
    sudo yum-config-manager --add-repo http://mirrors.aliyun.c
    sudo yum makecache fastom/docker-ce/linux/centos/docker-ce.repo
    
  • 修改sshd配置

    1
    2
    3
    
    vim /etc/ssh/sshd_config
    [修改内容]
    UseDNS=no
    

5.安装git

1
2
3
4
5
yum install -y git

git config --global credential.helper store
git config --global user.name "zkq"
git config --global user.email wdyxzkq@163.com

6.安装基础环境

1
2
3
4
5
6
7
8
yum install -y wget vim lrzsz unzip git dos2unix

vim /etc/profile
[新增内容]
PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ "
export EDITOR="/usr/bin/vim"

source /etc/profile

一个简单的脚本

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
cd /root
vi linux_init.sh
[内容 start]
#!/bin/bash
#update: 2022.04.23
#version: 1.0.24

#Global var
WEBDIR=/data/webserver
WEBSITEDIR=/data/website
WWWDIR=/data/www
TOOLSDIR=/data/tools
EXTDIR=/data/tools/php-ext
CONFDIR=/data/conf
LOGDIR=/data/log
BACKUPDIR=/data/backup
DBDIR=/data/database
APPDIR=/data/application
SCPDIR=/data/scp
LOGFILE=/data/log/appinstall.log
LOCALDIR=/data/website/linux_init

#check net_type
default_route=$(ip route show)
default_interface=$(echo $default_route | sed -e 's/^.*dev \([^ ]*\).*$/\1/' | head -n 1)
address=$(ip addr show label $default_interface scope global | awk '$1 == "inet" { print $2,$4}')

#ip addressw
ip=$(echo $address | awk '{print $1 }')

LOCALIP=${ip%%/*}
LOCALDIR=$(cd "$(dirname "$0")"&& pwd)

function base_env ()
{
rpm -qa|grep wget && rpm -qa|grep make && rpm -qa|grep gcc && rpm -qa|grep lftp && rpm -qa|grep gcc-c++ && rpm -qa|grep ntpdate && rpm -qa|grep unzip && rpm -qa|grep git- && rpm -qa|grep vim> /dev/null
if [ $? -eq 1 ]
then
yum install -y zlib-devel openssl-devel perl net-snmp lsof wget ntpdate make gcc gcc-c++ ncurses* telnet lftp openssh-clients vim lrzsz htop iftop net-tools unzip git vim dos2unix
git config –global core.editor vim
git config --global credential.helper store
echo -e "\e[1;32m基础环境已更新\e[0m"
fi

cat /etc/profile |grep PS1 >> /dev/null
if [ $? -eq 1 ]
then
  echo 'PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[36;40m\]\w\[\e[0m\]]\\$ "' >> /etc/profile
  echo 'HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile
  echo 'export EDITOR="/usr/bin/vim"' >> /etc/profile
fi

mkdir -p $WEBDIR
mkdir -p $WEBSITEDIR
mkdir -p $TOOLSDIR
mkdir -p $DBDIR
mkdir -p $APPDIR
mkdir -p $SCPDIR
mkdir -p $LOGDIR
mkdir -p $BACKUPDIR
mkdir -p $CONFDIR
mkdir -p $EXTDIR

if [ `getenforce` == 'Enforcing' ]
then
  setenforce 0
  sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
  sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
  echo -e "\e[1;32m已关闭selinux\e[0m"
fi
systemctl stop firewalld
echo -e "\e[1;32m已关闭系统防火墙\e[0m"

cp -pr ./tools/* $TOOLSDIR/
cp -pr ./conf/* $CONFDIR/
cp -pr ./code/* $WEBSITEDIR/
cp -pr ./scp/* $SCPDIR/
echo -e "\e[1;32m已安装工具包\e[0m"
}

#function end

echo -e "\e[1;32m======================================================\e[0m"
echo -e "\e[1;32m选择要安装的服务类型,目前支持以下项目:\e[0m"
echo -e "\e[1;36m(1): 创建规范目录,安装基础库环境,关闭selinux,配置时间同步
(q): 退出\e[0m"

echo -e "\e[1;32m请输入序号:\e[0m"
read options

function main ()
{
  case "$options" in

  1)
	  clear
	  base_env
          echo -e "\e[1;32m基础环境配置完毕\e[0m"
	  cd $LOCALDIR
	  sh ./init.sh
	  ;;
  q)
	  echo "退出脚本"
	  exit
	  ;;
  *)
	  clear
    echo -e "\e[1;31m请输入正确的选项\e[0m"
    cd $LOCALDIR
	  sh ./init.sh
esac
}

main $@
exit $?
[内容 end]
chmod +x linux_init.sh
./init.sh
选0 回车

7.安装linux_init项目

1
2
3
4
5
6
7
mkdir -p /data/website
cd /data/website
git clone https://gitee.com/dysoso/linux_init.git
cd linux_init
./init.sh

选1回车,安装基础环境

8.lnmp环境配置

1
使用linux_init项目(https://gitee.com/dysoso/linux_init.git)配置

9.docker和docker-compose环境

1
使用dnmp项目(https://gitee.com/dysoso/dnmp.git)配置

10.golang环境配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#安装Go
curl -R -O https://dl.google.com/go/go1.17.6.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf /data/tools/go1.17.6.linux-amd64.tar.gz
mkdir -p /root/go

#添加执行命令至环境变量
vim /etc/profile
【内容】
export GOROOT=/usr/local/go
export GOPATH=/root/go
export PATH=$PATH:$GOPATH/bin:$GOROOT/bin

source /etc/profile

#GOMOD和GOPROXY配置
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct