forked from lutaoact/script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_centos_dev.sh
234 lines (194 loc) · 7.93 KB
/
build_centos_dev.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
# 构建centos开发环境,基于如下版本:
# lsb_release -a #执行此命令,可以查看版本信息
# LSB Version: :core-4.1-amd64:core-4.1-noarch
# Distributor ID: CentOS
# Description: CentOS Linux release 7.2.1511 (Core)
# Release: 7.2.1511
# Codename: Core
## 一些必须要装的软件
yum -y update
yum -y install tmux && ln -s /usr/bin/true /etc/sysconfig/bash-prompt-screen #软链接是为了解决tmux窗口名称被莫名重置的问题
yum -y install krb5-devel #安装`mongoose`报错,`gssapi.h:No such file or directory`
#修改vi指向vim
mv /usr/bin/vi /usr/bin/vi_bak
ln -s /usr/bin/vim /usr/bin/vi
cd /root
useradd centos
echo 'ddxd2015' | passwd --stdin centos
# 为centos增加sudo权限
cp /etc/sudoers /etc/sudoers_$(date +'%Y%m%d%H%M%S').bak
cat << EOF >> /etc/sudoers
## custom configuration for centos
## Allow centos to run any commands anywhere
centos ALL=(ALL) ALL
EOF
#sed -i '/secure_path/{s#$#:/usr/local/bin#}' /etc/sudoers #安全路径中增加/usr/local/bin
# 禁用root用户登录
sed -i.bak '/PermitRootLogin/c PermitRootLogin no' /etc/ssh/sshd_config
service sshd restart
mkdir -p /data
mkdir -p /data/log
mkdir -p /data/backup
mkdir -p /data/redis
chown -R centos:centos /data
## 安装nginx
cat << 'EOF' > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=1
EOF
wget http://nginx.org/keys/nginx_signing.key
rpm --import nginx_signing.key
yum -y update && yum install -y nginx
cat << 'EOF' > /etc/nginx/conf.d/reverse-proxy.conf
server
{
listen 80;
server_name devapi.stockalert.cn;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9001;
set $cors '';
if ($http_origin ~* '(124.79.145.86|localhost|127.0.0.1)') {
set $cors 'true';
}
if ($cors = 'true') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
# add_header 'Access-Control-Allow-Headers' '*';
}
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With';
# add_header 'Access-Control-Allow-Headers' '*';
add_header 'Content-Type' 'text/plain';
add_header 'Content-Length' 0;
return 204;
}
}
access_log /data/log/devapi.stockalert.cn.nginx.access.log;
}
EOF
service nginx restart
## 安装mongodb
cat << 'EOF' > /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
EOF
yum -y update && yum install -y mongodb-org && service mongod start
## 通过源代码安装redis
yum install -y tcl #安装tcl依赖
cd /data/backup
wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar xvfz redis-3.0.7.tar.gz
cd redis-3.0.7
make
make test
make install
# 设置自动启动脚本
cd utils
./install_server.sh
# redis的相关配置
#Port : 6379
#Config file : /etc/redis/6379.conf
#Log file : /data/log/redis.log
#Data dir : /data/redis
#Executable : /usr/bin/redis-server
#Cli Executable : /usr/bin/redis-cli
# 修改服务名 redis_6379 => redis
cp /etc/init.d/redis_6379 /etc/init.d/redis
chkconfig --add redis
chkconfig --del redis_6379
\rm /etc/init.d/redis_6379
service redis restart
## CENTOS_CMD中的内容由centos用户来执行
read -r -d '' CENTOS_CMD << 'HERE_DOC'
mkdir ~/.ssh
chmod -R 700 ~/.ssh
cat << EOF > ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4EWdOtLSaqEWpUYmdN0FwsTqyHBItiJLXj10zrHHQVoa7AU1DFQFNnXglEtXFRsxBYc0uPkl5ib77LV1npUkZCg9LTbjoz8LVkIHGOXkjrBLt6QyZZYFZm6RQ8OoiGSwYvsy2zCsK5SRVruqmcDznYawHacI1mF+u6PSwiubM8FgQe+c3sUyOTF8Thp0Wb6nXx/c75JD+NlSjc6kEnh7Fb2EYsYIDog+rhMS+QXnAjt4pEstouKq2Mci0LLdrJbuam9RSfChbowpUWe/JAck5qG5HPrZDm7H8AjoTOeNlLgC/Vg3C5qZdfkCUpjC1G0IXjBQWFbQJfMQSwJVTOYQJ [email protected]
EOF
chmod 600 ~/.ssh/authorized_keys
cat << 'EOF' >> ~/.bashrc
# custom configuration for centos
if [ "$PS1" ]; then
complete -cf sudo
fi
export EDITOR=vim
export VISUAL=vim
export NODE_ENV=development
alias npm='npm --registry=https://registry.npm.taobao.org'
alias redis-cli='redis-cli --raw' #让redis-cli正常显示中文
EOF
# 检查环境,必须装有sslocal和polipo,否则直接退出
# command -v sslocal >/dev/null 2>&1 || { echo >&2 "require sslocal but it's not installed. Aborting."; exit 1; }
# command -v polipo >/dev/null 2>&1 || { echo >&2 "require polipo but it's not installed. Aborting."; exit 1; }
# export http_proxy=http://127.0.0.1:8123 #需要先安装polipo的代理
# 利用nvm安装node
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
. ~/.bashrc #使nvm命令生效
nvm install 4.4
nvm alias default 4.4
npm install --verbose -g pm2
## clone
cd ~
git clone https://github.com/lutaoact/some_config.git
ln -sf ~/some_config/vimrc ~/.vimrc
ln -sf ~/some_config/gitconfig ~/.gitconfig
sed -i 's/osxkeychain/cache --timeout=864000/' ~/some_config/gitconfig
sed -i '/http\|proxy/d' ~/some_config/gitconfig
# 导入bitbucket的key,然后clone node-server库
cat << EOF > ~/.ssh/bitbucket.job.pem
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA0spqj8FAcHxoOjhbqenKFbiUUGdmHs1rqud1xBgaSnJ2uu0w
SylcwFQzdwsj52s3XkNrs2WljJxQArr+tLShjiTeFpfr6s2I6W+RA8eWheW8MZlt
s8G9e0l8iFtuKDIywbqgKhEfUaxcaA7Jnxht0jjgEE3c50g+ZykCXcPXvN/almHM
jpCu7ibDEwtXwdB6hbDswtdLyK8L9I9eZr0BoSuTb2oAI5lc/8x6ZjBi9Hr4PDWn
EZcTLv0Nm7Gv3fRmBmnXjWsdkCY5rEI7nqJOXeBRt4WrT0pNgi7knmVHLczvPi63
4OMEWizkYmNwEds1E/W8AUNYI9LF114AycDUHQIDAQABAoIBAAS1cNv1PTNGFC34
xF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rsXo4IKeWn9ZG2kolKF9mqWzM79hN
cE3Sa+YD1/W3P6XWCRg5c2Xb3MTfMz3DrNHu5UECKf8oxPI7biYwy94fXq1aCeS8
4g8AO4F5n8l1LZSTpov9YxNrJm8M2LmDIJlNDYBpPhVlXZKwJEKujOw=
-----END RSA PRIVATE KEY-----
EOF
cat << EOF > ~/.ssh/config
ServerAliveInterval 60
Host bitbucket.org
Hostname bitbucket.org
User git
IdentityFile ~/.ssh/bitbucket.job.pem
StrictHostKeyChecking no
EOF
chmod 600 ~/.ssh/*
git clone [email protected]:lutaoact/node-server.git
HERE_DOC
su --login centos -c "$CENTOS_CMD" #以centos用户来执行命令