centos7两行命令部署测试环境(mysql+redis+nginx+jdk+mongodb)

时间:2020-9-3 作者:admin

最近很多博友私聊我说,文章写得简单易懂,也运行起来了,但是还是觉得麻烦,希望一个命令就部署完服务器环境。整合爬坑不易,您的点赞和关注是最大的动力。更多干货在来的路上,谢谢您的支持

1. 主题

  • 整合 nginx:1.18.0
  • 整合 mysql:5.7.30
  • 整合 redis:5.0.9
  • 整合 mongodb:4.4.0
  • 整合 jdk:8

2. 事前准备

  • 干净的centos7环境,centos7 镜像下载链接:http://mirrors.aliyun.com/centos/7/isos/x86_64/
  • 提前下载百度云已下载好的文件,我已经将主题内的软件包下载并上传到了百度云:https://pan.baidu.com/s/1-Kff-Q1k9KCHca7jpQB9iA 密码: dq7n

3.文件包含

 

4.执行脚本(执行2步命令,等待运行完成,服务器环境就安装完啦)

sh /home/sh/install.sh jdk nginx redis mysql mongodb
source /etc/profile

5.题外话

jdk 安装脚本

#!/bin/bash
# 这里要的jdk要写成你自己的版本,要和上传的对应
tar -zxvf /home/service/jdk-8u161-linux-x64.tar.gz -C /home/service
mv /home/service/jdk1.8.0_161 /home/service/java
 
# 文件追加,注意$PATH会被解析,
# 方案1:加转义字符,例子(\$PATH)
# 方案2:EOF 加反斜杠,或用单引号、双引号包围 。适用变量多的情况
cat <<'EOF'>> /etc/profile
# 设置java 环境
export JAVA_HOME=/home/service/java
export JRE_HOME=/home/service/java/jre
export CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
EOF

nginx 安装脚本

#!/bin/bash
yum -y install gcc pcre pcre-devel zlib openssl openssl-devel

tar -xzvf /home/service/nginx-1.18.0.tar.gz -C /home/service
cd /home/service/nginx-1.18.0
./configure --prefix=/home/service/nginx --with-http_ssl_module
make install

mkdir -p /home/service/nginx/myconfig/
mkdir -p /home/service/nginx/myconfig/conf.d
mkdir -p /home/service/nginx/mylogs/

# 创建nginx配置文件
ln -s /home/service/nginx/conf/nginx.conf /home/service/nginx/myconfig/nginx.conf
cat <<'EOF'> /home/service/nginx/myconfig/nginx.conf
#user nginx nginx;
#user root;
worker_processes  auto;
 
error_log  /home/service/nginx/mylogs/error.log warn;
#pid        /var/run/nginx.pid;
 
worker_rlimit_nofile 65535;
events {
    use epoll;
        worker_connections  65535;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    fastcgi_intercept_errors on;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /home/service/nginx/mylogs/access.log  main;
    limit_conn_zone $binary_remote_addr zone=conn:10m;
    limit_req_zone $binary_remote_addr zone=allips:10m rate=50r/s;
 
    server_names_hash_bucket_size 128;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    client_max_body_size 20m;
    client_body_timeout 120;
    server_tokens off;
    sendfile        on;
    tcp_nopush     on;
 
    keepalive_timeout  65;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_comp_level 2;
    gzip_vary on;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    
    include /home/service/nginx/myconfig/conf.d/*.conf;
}
EOF
 
# 软链
mkdir -p /home/config
ln -s /home/service/nginx/myconfig /home/config/nginx
mkdir -p /home/logs
ln -s /home/service/nginx/mylogs /home/logs/nginx


cat <<'EOF'> /home/service/nginx/myconfig/conf.d/nginx.conf
#此配置文件为websocket
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
 
#upstream dist {
    #server  127.0.0.1:8123;
#}
 
#80端口转向443
#server {
  #listen 80;
  #server_name 域名.com,www.域名.com;
  #return 301 https://www.域名.com$request_uri;
#}
 
#非www重定向www
#server {
 #listen 443;
 #server_name 域名.com;
 #return 301 https://www.域名.com$request_uri;
#}
 
server {
        listen       80;
        #listen       443 default_server ssl;
        #server_name  www.域名.com;
        server_name  localhost;
        root /home/service/nginx/html;
        index index.html index.htm;
        
        #location ~*\.(gif|jpg|jpeg|png|bmp|swf|css|js|html)$ {
        #   root /home/service/nginx/html;
        #}
 
        #下载目录
        #location /download {
            #rewrite ^/download/(.*)$ /$1 break;
            #add_header   Content-Type     "application/octet-stream;charset=utf-8";
            #add_header   Content-Disposition "attachment; filename*=utf-8'zh_cn'$arg_n";
            #root /home/www/html/downloadF;
            #autoindex on;
            #autoindex_exact_size off;
        #}
 
         #配置https
         #ssl_certificate /home/config/nginx/sslfile/1_www.域名.com_bundle.crt;
         #ssl_certificate_key /home/config/nginx/sslfile/2_www.域名.com.key;
         #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
         #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
         #ssl_prefer_server_ciphers on;
         #ssl_session_cache   shared:SSL:10m;
         #ssl_session_timeout 10m;
         #ssl_buffer_size 1400;
 
location /api {
                 proxy_http_version 1.1;
                 proxy_set_header Connection "";
                 proxy_connect_timeout 60s;
                 proxy_read_timeout 60s;
                 proxy_send_timeout 60s;
                 proxy_buffer_size 512k;
                 proxy_buffering  on;
                 proxy_buffers   8 512k;
                 proxy_busy_buffers_size 1024k;
                 proxy_max_temp_file_size 100M;
                 proxy_pass http://127.0.0.1:8080; #http://dist
                 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_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection 'upgrade';
                 proxy_set_header Origin "";
                 #限制TCP并发连接数100/s
                 limit_conn conn 100;
         }
}
EOF


cat <<'EOF'> /home/service/nginx/myconfig/nginx.service
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/home/service/nginx/sbin/nginx -c /home/service/nginx/conf/nginx.conf
ExecReload=/home/service/nginx/sbin/nginx restart
ExecStop=/home/service/nginx/sbin/nginx  stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

cp /home/config/nginx/nginx.service /lib/systemd/system/nginx.service
 
systemctl enable nginx.service
systemctl start nginx.service
service nginx status

mysql 安装脚本

#!/bin/bash

# 解压mysql文件 
tar -zxvf /home/service/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /home/service
mv /home/service/mysql-5.7.30-linux-glibc2.12-x86_64 /home/service/mysql

mkdir -p /home/service/mysql/myconfig
mkdir -p /home/service/mysql/mylogs

# 创建mysql配置文件
cat <<'EOF'> /home/service/mysql/myconfig/my.cnf
[mysqld]
character_set_server=utf8mb4
basedir=/home/service/mysql
datadir=/home/service/mysql/data
port = 3306
log-error=/home/service/mysql/mylogs/mysqld.log
#不开启sql严格模式
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
slow_query_log=ON
slow_query_log_file=/home/service/mysql/mylogs/slowquery.log
long_query_time=3
log-queries-not-using-indexes=ON
lower_case_table_names=1
EOF

mkdir -p /home/config
mkdir -p /home/logs
ln -s /home/service/mysql/myconfig /home/config/mysql
ln -s /home/service/mysql/mylogs /home/logs/mysql

# 创建mysql安装脚本
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql /home/service/mysql
cp -rf /home/service/mysql/myconfig/my.cnf /etc/my.cnf
/home/service/mysql/bin/mysqld --initialize --user=mysql --datadir=/home/service/mysql/data --basedir=/home/service/mysql
cp /home/service/mysql/support-files/mysql.server /etc/init.d/mysql
sed -i "s/^basedir=$/basedir=\/home\/service\/mysql/g" /etc/init.d/mysql
sed -i "s/^datadir=$/datadir=\/home\/service\/mysql\/data/g" /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
chkconfig --level 345 mysql on
service mysql start


cat <<'EOF'>> /etc/profile
export MYSQL_HOME=/home/service/mysql
export PATH=$PATH:$MYSQL_HOME/bin
EOF

# 查看初始密码
grep 'password' /home/service/mysql/mylogs/mysqld.log

redis 安装脚本

#!/bin/bash
yum -y install gcc
tar -xzvf /home/service/redis-5.0.9.tar.gz -C /home/service/
mv /home/service/redis-5.0.9 /home/service/redis
cd /home/service/redis
make

mkdir -p /home/config
mkdir -p /home/service/redis/myconfig
cp /home/service/redis/redis.conf /home/service/redis/myconfig/
ln -s /home/service/redis/myconfig /home/config/redis
sed -i "s/daemonize no/daemonize yes/g" /home/service/redis/myconfig/redis.conf
sed -i "s/# requirepass foobared/requirepass 123456/g" /home/service/redis/myconfig/redis.conf
 
cat <<'EOF'> /home/service/redis/myconfig/redis.service
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/home/service/redis/src/redis-server /home/service/redis/myconfig/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
EOF
 
cp /home/service/redis/myconfig/redis.service /lib/systemd/system/redis.service

systemctl enable redis.service
systemctl start redis.service
systemctl status redis.service

cat <<'EOF'>> /etc/profile
export REDIS_HOME=/home/service/redis
export PATH=$PATH:$REDIS_HOME/src
EOF

mongodb 安装脚本

#!/bin/bash
tar -xzvf /home/service/mongodb-linux-x86_64-rhel70-4.4.0.tgz -C /home/service/
mv /home/service/mongodb-linux-x86_64-rhel70-4.4.0 /home/service/mongodb

mkdir -p /home/service/mongodb/myconfig
ln -s /home/service/mongodb/myconfig /home/config/mongodb
cat <<'EOF'> /home/service/mongodb/myconfig/mongodb.conf
dbpath = /home/service/mongodb/data/db
logpath = /home/service/mongodb/data/logs/mongodb.log
port = 27017
# 测试下可以0.0.0.0,正式环境:127.0.0.1
bind_ip=0.0.0.0
fork = true
#nohttpinterface = true
auth=true
EOF
 
# 软链
ln -s /home/service/mongodb/data/logs /home/logs/mongodb

mkdir -p /home/service/mongodb/data/db
mkdir -p /home/service/mongodb/data/logs
 
cat <<'EOF'> /home/service/mongodb/myconfig/mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/service/mongodb/bin/mongod --config /home/service/mongodb/myconfig/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/service/mongodb/bin/mongod --shutdown --config /home/service/mongodb/myconfig/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
 
cp /home/service/mongodb/myconfig/mongodb.service /lib/systemd/system/mongodb.service
systemctl enable mongodb.service
systemctl start mongodb.service
service mongodb status

cat <<'EOF'>> /etc/profile
export MONGODB_HOME=/home/service/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
EOF

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

 

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。