2018年2月5日 星期一

Bash Script 自動安裝LNMP 並設定密碼 (CentOS7)

最近一直在做測試機,而且看到大陸有做一鍵完整包,奈何眾所皆知的問題...乾脆自己寫一個
順便練習script
很粗糙,有空更新會一直更新下去

1.使用方法就是準備一台Centos 7 並設定好網路,
2.把下面的東西拷貝下去找個記事本存成.sh,然後尋找1.1.1.1 替換成你的IP位址
3.放到機器上執行  例如: su root ./test.sh

#要注意的是 輸入新密碼是明碼顯示,而且沒有確認訊息
使用程式版本




#!/bin/bash
#echo 'start install software'
yum -y install epel-release
yum -y install php php-fpm nginx phpmyadmin mariadb mariadb-server expect

set timeout 3
#echo 'set firewall'
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

#echo 'set shortcut for phpmyadmin'
ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/html/
chown nginx:nginx /var/lib/php/session

cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak
sed -i 's/;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf

cp /etc/phpMyAdmin/config.inc.php /etc/phpMyAdmin/config.inc.php.bak
sed -i "s/'cookie'/'http'/" /etc/phpMyAdmin/config.inc.php

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cat > /etc/nginx/nginx.conf <<'EOF'

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 5;
error_log /var/log/nginx/error.log;

pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 10240;
}
http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       1.1.1.1:80;
        server_name  1.1.1.1;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
            root   html;
     index  index.php index.html index.htm;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
}

EOF

#echo 'set and start the service'
systemctl enable nginx mariadb php-fpm
systemctl restart nginx mariadb php-fpm

read -p"input password: " password
expect -f - <<-EOF

set timeout 10
spawn mysql_secure_installation
expect "Enter current password for root (enter for none):"
send -- "\r"
expect "Set root password?"
send -- "y\r"
expect "New password:"
send -- "$password\r"
expect "Re-enter new password:"
send -- "$password\r"
expect "Remove anonymous users?"
send -- "y\r"
expect "Disallow root login remotely?"
send -- "y\r"
expect "Remove test database and access to it?"
send -- "y\r"
expect "Reload privilege tables now?"
send -- "y\r"
expect eof

EOF

yum -y autoremove expect

沒有留言:

張貼留言