lnmp自动安装脚本

#! /bin/bash

#1、安装nginx
#安装环境
yum install -y  gcc  zlib  zlib-devel  pcre pcre-devel openssl openssl-devel wget gcc-c++

#到/opt/下
cd /opt/
wget http://nginx.org/download/nginx-1.20.1.tar.gz
#解压
gunzip nginx-1.20.1.tar.gz
#解压
tar -xvf nginx-1.20.1.tar

#到解压目录下
cd nginx-1.20.1

#创建用户组
useradd  nginx -s  /sbin/nologin

#编译参数
./configure --user=nginx  --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_v2_module  --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-stream --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf

make && make install

nginx

yum install -y net-tools

cd /usr/local/nginx/conf
mv /usr/local/nginx/conf/nginx.conf nginx.conf.bak


#写一个.conf模板
cat >/usr/local/nginx/conf/nginx.conf  <<EOF

user nginx nginx;
worker_processes auto;

#error_log /var/log/nginx/error.log crit;
#pid /var/run/nginx.pid;
worker_rlimit_nofile 204800;

events {
    use epoll;
    worker_connections 204800;
    multi_accept on;
    }

http {
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 1024m;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 60;
    server_tokens off;
    tcp_nodelay on;
    
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 60;
    fastcgi_read_timeout 60;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 128k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;

    #Gzip Compression
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 6;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

#log_format weblog       '$remote_addr - $remote_user [$time_local] "$request" '
#                        '$status $body_bytes_sent "$http_referer" '
#                        '"$http_user_agent $http_x_forwarded_for"';

log_format weblog	'[$time_local] "$http_x_forwarded_for" "$http_user_agent"'
			'--$remote_addr $remote_user'
                	'"$request" $status $body_bytes_sent "$http_referer" ';

######################## default ############################
#    server {
#    listen 80;
#    server_name _;
#    access_log /data/wwwlogs/access_nginx.log combined;
#     root /usr/local/nginx/html;
#     index index.html index.htm index.php;
#    location /nginx_status {
#        stub_status on;
#        access_log off;
#        allow 127.0.0.1;
#        deny all;
#        }
#     location ~ [^/]\.php(/|$) {
#         fastcgi_pass 127.0.0.1:9000;
#         fastcgi_pass unix:/dev/shm/php-cgi.sock;
#	 index index.php;
#         fastcgi_index index.php;
#         include fastcgi.conf;
#         }
#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
#        expires 30d;
#        access_log off;
#        }
#    location ~ .*\.(js|css)?$ {
#        expires 7d;
#        access_log off;
#        }
#	location /logs/ {
#		auth_basic            "Restricted";
#		auth_basic_user_file  htpasswd.nginx;
#		alias  /home/wwwlogs/api.store.etcchebao.com/openlog/;
#		autoindex on;
#		autoindex_exact_size off;
#		autoindex_localtime on;
#    }
#}
#
########################## vhost #############################
    include vhost/*.conf;
    include vhost/https/*.conf;
    #include /home/www/nginx_vhosts/*/*.conf;
    #include vhost/conf_n/*.conf;
}
EOF

#创建vhost目录,此后server在里面手撕
mkdir /usr/local/nginx/conf/vhost
cd /usr/local/nginx/conf/vhost

#马上手撕一个测试用的server
cat > /usr/local/nginx/conf/vhost/test.conf <<END
server {
    listen 80;
    server_name _;
    root /usr/local/nginx/html;
    index index.html index.htm index.php;
       }
END
#关闭防火墙
systemctl stop firewalld

nginx -t
nginx -s reload
#更改A记录,即可在浏览器输入虚拟机ip访问到nginx页面
#################################################################
#2安装mysql5.7
#使用rz命令把mysql-5.7.35-el7-x86_64.tar上传到/opt 下,或者wget一个安装包到/opt下
#包名一定要是mysql-5.7.35-el7-x86_64.tar,不然脚本用不了
#包的目录一定是/opt


#首先移除历史环境,否则影响mysqld的安装
yum remove mariadb-libs -y

#安装环境,否则mysql输入显示error while loading shared libraries: libncurses.so.5: cannot open shared object file
yum install libncurses* -y

##创建用户和组
useradd mysql -s /sbin/nologin


##创建数据储存目录
mkdir -p /data/mysqldata

##创建日志目录
mkdir -p /var/log/mysql

##设置权限
#chown mysql:mysql -R /usr/local/mysql
chown mysql:mysql -R /data/mysqldata
chown mysql:mysql -R /var/log/mysql


##到放安装包的目录
cd /opt/

##解压tar包
#tar -xvf mysql-5.7.35-el7-x86_64.tar

##删除tar包,因为gunzip mysql-5.7.35-el7-x86_64.tar.gz解压之后,也有个包叫mysql-5.7.35-el7-x86_64.tar,不删要手动覆盖
#rm -rf mysql-5.7.35-el7-x86_64.tar

##再次确认安装gzip命令
yum install -y gzip

##解压.gz包
gunzip mysql-5.7.35-el7-x86_64.tar.gz

#解压tar包,!!注意这不是之前的tar包!!只是和之前同名
tar -xvf mysql-5.7.35-el7-x86_64.tar

#解压mysql-5.7.35-el7-x86_64.tar后得到一个目录,将此目录移动到/usr/local/下,并命名为mysql
mv mysql-5.7.35-el7-x86_64 /usr/local/mysql

##修改所属用户和组为mysql
chown mysql:mysql -R /usr/local/mysql

##初始化数据库(不生成临时密码)
/usr/local/mysql/bin/mysqld --initialize-insecure  --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata

##配置文件 /etc/my.cnf
echo -e "[mysqld]\nuser=mysql\ndatadir=/data/mysqldata\nserver_id=6\nlog-error=/var/log/mysql/error.log\npid-file=/data/mysqldata/mysql.pid\nport=3306\nsocket=/tmp/mysql.sock\n[mysql]\nsocket=/tmp/mysql.sock" >> /etc/my.cnf

##启动mysql服务
/usr/local/mysql/support-files/./mysql.server start

##复制启动脚本生产系统命令,并添加到systemctl管理
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

#做一个软连接,LINUX默认命令放在/usr/bin下,不做找不到mysql命令
ln -s /usr/local/mysql/bin/mysql /usr/bin
####################################################################################
#安装php
#! /bin/bash

cd /opt
yum install -y wget

#安装依赖
yum install gcc            -y
yum install gcc-c++        -y
yum install freetype-devel -y
yum install openssl-devel  -y
yum install openssl        -y
yum install curl-devel     -y
yum install libxml2        -y
yum install libxml2-devel  -y
yum install libjpeg-devel  -y
yum install libpng-devel   -y
yum install libXpm-devel   -y
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure

make && make install

cd /opt
wget https://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz
# wget https://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz --no-check-certificate
tar zxf re2c-0.16.tar.gz
cd re2c-0.16
./configure
make
make install

cd /opt
wget http://192.168.3.200/package/gz/php-7.0.27.tar.gz

#yum install -y gunzip
yum install curl-devel     -y
#gunzip php-7.0.27.tar.gz

tar -zxf php-7.0.27.tar.gz

cd /opt/php-7.0.27


./configure --prefix=/usr/local/php         --with-config-file-path=/usr/local/php/etc         --with-config-file-scan-dir=/usr/local/php/etc/php.d         --disable-ipv6         --enable-bcmath         --enable-calendar         --enable-exif         --enable-fpm         --with-fpm-user=www         --with-fpm-group=www         --enable-ftp         --enable-gd-jis-conv         --enable-gd-native-ttf         --enable-inline-optimization         --enable-mbregex         --enable-mbstring         --enable-mysqlnd         --enable-opcache         --enable-pcntl         --enable-shmop         --enable-soap         --enable-sockets         --enable-static         --enable-sysvsem         --enable-wddx         --enable-xml         --with-curl         --with-gd         --with-jpeg-dir         --with-freetype-dir         --with-xpm-dir         --with-png-dir         --with-gettext         --with-iconv         --with-libxml-dir         --with-mcrypt         --with-mhash         --with-mysqli         --with-pdo-mysql         --with-pear         --with-openssl         --with-xmlrpc         --with-zlib         --disable-debug         --disable-phpdbgnable-sockets         --enable-static         --enable-sysvsem         --enable-wddx         --enable-xml         --with-curl         --with-gd         --with-jpeg-dir         --with-freetype-dir         --with-xpm-dir         --with-png-dir         --with-gettext         --with-iconv         --with-libxml-dir         --with-mcrypt         --with-mhash         --with-mysqli         --with-pdo-mysql         --with-pear         --with-openssl         --with-xmlrpc         --with-zlib         --disable-debug         --disable-phpdbg


make && make install

cp /opt/php-7.0.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm

mkdir -p /home/wwwlogs
useradd www -s /sbin/nologin


#创建php-fpm.conf和php.ini
cd /usr/local/php/etc
wget http://192.168.3.200/config/php-fpm.conf
wget http://192.168.3.200/config/php.ini

#先关掉模块增加,有需要时再开启
sed -i  's/extension=swoole.so/#extension=swoole.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=mongodb.so/#extension=mongodb.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=yar.so/#extension=yar.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=redis.so/#extension=redis.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=yaconf.so/#extension=yaconf.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=zip.so/#extension=zip.so/g' /usr/local/php/etc/php.ini 
sed -i  's/zend_extension=\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20151012\/opcache.so/#zend_extension=\/usr\/local\/php\/lib\/php\/extensions\/no-debug-non-zts-20151012\/opcache.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=imagick.so/#extension=imagick.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=msgpack.so/#extension=msgpack.so/g' /usr/local/php/etc/php.ini 
sed -i  's/extension=ssh2.so/#extension=ssh2.so/g' /usr/local/php/etc/php.ini 

#启动
/etc/init.d/php-fpm start

if [ $? -eq 0 ]
	then
clear
read -p "现在你可选择 
(A)nginx增加防盗链功能
(B)nginx平滑升级到1.21
(C)增加php第三方redis模块功能
(D)更多第三方模块敬请期待
##按Enter键退出,不选择扩展,并完成安装。"  str
		
		if test -z "$str"
		then
		echo "你已经完成lnmp安装"
		exit 0
		fi

	case $str in 
		a|A)
			 mkdir -p /data/gz.com
             cd /usr/local/nginx/conf/vhost
             rm -rf test.conf
                cat > gz.conf <<EOF
server {
    listen 80;
    server_name _;

    index index.html index.htm index.php;

	location / {
        root /data/gz.com;
        }

 location = /images/no.jpg {
        root  /data/gz.com;
        }

location ~  .*\.(jpg|jpeg|gif|png|webp)$ {
              valid_referers  none  blocked server_names
                  *.gz.com;

             if (\$invalid_referer) {
               rewrite  .*  http://www.gz.com/images/no.jpg;
              }
            root  /data/gz.com;  
        }
       }
EOF


		;;    
		b|B)
            cd /opt/
			wget http://nginx.org/download/nginx-1.21.3.tar.gz
            tar -xvf nginx-1.21.3.tar.gz
            cd nginx-1.21.3
            cp /usr/sbin/nginx /usr/sbin/nginx.bak
            ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-stream --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf
            make
            \cp -f objs/nginx /usr/sbin/nginx            
            a=$(ps -ef |grep nginx|grep master|awk -F " " '{print $2}')
            kill -USR2  $a
            kill -WINCH $a
            kill -QUIT  $a
            nginx
		;;
		c|C)
		cd /opt/
		wget https://pecl.php.net/get/redis-5.3.2.tgz
		tar -xvf redis-5.3.2.tgz
		cd redis-5.3.2
		yum install autoconf -y
		/usr/local/php/bin/phpize
		./configure --with-php-config=/usr/local/php/bin/php-config	
		make
        make inatall
 		sed -i  's/#extension=redis.so/extension=redis.so/g' /usr/local/php/etc/php.ini 
		/etc/init.d/php-fpm restart
		
		;;
		*)
			echo "您只能选择ABC或退出"
		;;
		esac
		exit 0


fi	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值