1 #!/bin/bash 2 #用于安装LNMP环境 3 4 [ ! -d /software/ ] && mkdir /software 5 6 #功能选择菜单 7 menu(){ 8 echo -e "\033[31;32m LNMP编译安装脚本 \033[0m" 9 echo -e "\033[31;32m================================================================================\033[0m" 10 echo -e "\033[34m 此脚本能直接在rhel7.5、Centos 7.5上成功执行 \033[0m" 11 echo -e "\033[33m 安装包版本: nginx:1.14.0 mysql5.7.23 php7.2.6 \033[0m" 12 echo " 相关依赖包:pcre-devel、gd-devel、openssl、openssl-devel、php-mysql、bzip2 " 13 echo " perl-devel、libxml2-devel bzip2-devel libcurl-devel " 14 echo -e "\033[47;34m------------安装需联网下载软件包,若下载地址失效,需自行更新下载地址------------\033[0m" 15 echo -e "\033[31;32m================================================================================\033[0m" 16 echo -e "\033[33m nginx安装目录:/usr/local/nginx,日志目录:/data/logs/nginx/ \033[0m" 17 echo -e "\033[33m mysql安装目录:/usr/local/mysql,数据存放目录:/data/mysql/ \033[0m" 18 echo -e "\033[33m php安装目录:/usr/local/php7 \033[0m" 19 echo -e "\033[31;32m================================================================================\033[0m" 20 echo -e "\033[34m请选择:\033[0m" 21 echo -e "\033[36m0、安装依赖包 1、安装nginx 2、源码编译安装mysql 3、安装php \033[0m" 22 echo -e "\033[36m4、整合nginx和php 5、启动nginx、mysql、php-fpm服务 \033[0m" 23 echo -e "\033[36m6、一键安装并部署lnmp 7、退出脚本 \033[0m" 24 echo -e "\033[31;32m================================================================================\033[0m" 25 echo 26 read -p "请输入数字:0-5[单独安装](单独安装需要先安装依赖包),6[一键安装],7[退出脚本]: " num 27 } 28 29 #(0)安装依赖包 30 install_package(){ 31 cd /software 32 33 yum install -y vim-enhanced lrzsz net-tools gcc rsync epel-release wget unzip curl 34 yum install -y bzip2-devel openssl-devel gnutls-devel gcc gcc-c++ cmake ncurses-devel bison-devel libaio-devel openldap openldap-devel 35 yum install -y autoconf bison libxml2-devel libcurl-devel libevent libevent-devel gd-devel expat-devel numactl 36 37 if [ ! -f libmcrypt-2.5.8.tar.gz ] 38 then 39 wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz 40 fi 41 42 [ ! -d libmcrypt-2.5.8 ] && tar -zxf libmcrypt-2.5.8.tar.gz 43 cd libmcrypt-2.5.8 44 ./configure && make && make install 45 46 if [ $? -eq 0 ] 47 then 48 sleep 3 49 echo -e "\033[36m==========依赖包安装成功==========\033[0m" 50 else 51 echo -e "\033[31m**********安装依赖包失败,请检查**********\033[0m" 52 fi 53 } 54 55 #(1)编译安装nginx 56 install_nginx(){ 57 cd /software 58 59 #1、新建nginx系统用户 60 id nginx &> /dev/null 61 [ $? -ne 0 ] && useradd -r -s /sbin/nologin nginx 62 63 #2、新建nginx日志存放目录 64 [ ! -d /data/logs/nginx/ ] && mkdir -pv /data/logs/nginx/ && chown -R nginx:nginx /data/logs/nginx/ 65 66 #3、定义nginx安装的版本 67 NGINX_VERS=nginx-1.14.0 68 69 #4、下载nginx 70 if [ ! -f $NGINX_VERS.tar.gz ] 71 then 72 echo -e "\033[34m==========正在下载nginx源码包==========\033[0m" 73 curl -O http://nginx.org/download/$NGINX_VERS.tar.gz 74 fi 75 76 #5、解压下载包 77 [ ! -d $NGINX_VERS ] && tar xf $NGINX_VERS.tar.gz 78 79 #6、进入nginx解压后的目录 80 cd $NGINX_VERS 81 82 #7、编译安装 83 ./configure --prefix=/usr/local/nginx \ 84 --user=nginx \ 85 --group=nginx \ 86 --http-log-path=/data/logs/nginx/access.log \ 87 --error-log-path=/data/logs/nginx/error.log \ 88 --with-http_ssl_module \ 89 --with-http_realip_module \ 90 --with-http_flv_module \ 91 --with-http_mp4_module \ 92 --with-http_gunzip_module \ 93 --with-http_gzip_static_module \ 94 --with-http_image_filter_module \ 95 --with-http_stub_status_module && make && make install 96 97 if [ $? -eq 0 ] 98 then 99 sleep 3 100 echo -e "\033[36m==========nginx编译安装成功==========\033[0m" 101 else 102 echo -e "\033[31m**********nginx编译安装失败,请检查**********\033[0m" 103 exit 1 104 fi 105 106 cat > /usr/lib/systemd/system/nginx.service << EOF 107 [Unit] 108 Description=The nginx HTTP and reverse proxy server 109 After=network.target remote-fs.target nss-lookup.target 110 111 [Service] 112 Type=forking 113 PIDFile=/run/nginx.pid 114 # Nginx will fail to start if /run/nginx.pid already exists but has the wrong 115 # SELinux context. This might happen when running \`nginx -t\` from the cmdline. 116 # https://bugzilla.redhat.com/show_bug.cgi?id=1268621 117 ExecStartPre=/usr/bin/rm -f /run/nginx.pid 118 ExecStartPre=/usr/local/nginx/sbin/nginx -t 119 ExecStart=/usr/local/nginx/sbin/nginx 120 ExecReload=/bin/kill -s HUP \$MAINPID 121 KillSignal=SIGQUIT 122 TimeoutStopSec=5 123 KillMode=process 124 PrivateTmp=true 125 126 [Install] 127 WantedBy=multi-user.target 128 EOF 129 systemctl enable nginx && systemctl daemon-reload 130 } 131 132 #(2)源码编译安装mysql 133 install_mysql(){ 134 cd /software 135 136 VERSION=5.7.23 #mysql版本 137 138 if [ ! -f mysql-$VERSION.tar.gz ] 139 then 140 #下载mysql 141 echo -e "\033[34m==========下载mysql源码包==========\033[0m" 142 curl -o mysql-$VERSION.tar.gz https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-$VERSION-linux-glibc2.12-x86_64.tar.gz 143 fi 144 145 # 1、创建mysql用户 146 id mysql &> /dev/null 147 [ $? -ne 0 ] && useradd -r -s /sbin/nologin mysql 148 149 #2、解压mysql 150 echo -e "\033[34m==========解压mysql...==========\033[0m" 151 152 if [ -f mysql-$VERSION.tar.gz ] 153 then 154 tar xvf mysql-$VERSION.tar.gz 155 mv mysql-$VERSION-* /usr/local/mysql 156 fi 157 158 #3、创建数据库数据存放目录、安装目录 159 [ ! -d /data/mysql/ ] && mkdir -pv /data/mysql/ 160 [ ! -f /usr/local/mysql/log/error.log ] && mkdir -pv /usr/local/mysql/log/ && touch /usr/local/mysql/log/error.log 161 chown -R mysql:mysql /data/mysql/ 162 chown -R mysql:mysql /usr/local/mysql/ 163 164 #4、初始化 165 166 cd /usr/local/mysql 167 168 if [ $? -eq 0 ] 169 then 170 echo -e "\033[34m==========数据库开始初始化==========\033[0m" 171 ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/ 172 if [ $? -eq 0 ] 173 then 174 echo -e "\033[36m==========数据库初始化成功==========\033[0m" 175 else 176 echo -e "\033[31m**********编译安装错误!初始化失败**********\033[0m" 177 exit 1 178 fi 179 fi 180 181 #5、配置数据库配置文件 182 cat > /etc/my.cnf << EOF 183 [client] 184 port = 3306 185 socket = /usr/local/mysql/tmp/mysql.sock 186 187 [mysql] 188 default-character-set=utf8 189 190 [mysqld] 191 default-storage-engine=INNODB 192 character_set_server=utf8 193 explicit_defaults_for_timestamp 194 basedir=/usr/local/mysql/ 195 datadir=/data/mysql/ 196 socket=/usr/local/mysql/tmp/mysql.sock 197 log_error = /usr/local/mysql/log/error.log 198 199 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 200 EOF 201 202 [ ! -f /etc/init.d/mysql ] && \cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 203 204 sed -i '46s/basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysql 205 sed -i '47s/datadir=/datadir=\/data\/mysql/' /etc/init.d/mysql 206 207 chmod +x /etc/init.d/mysql 208 209 #6、配置环境变量 210 n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l` 211 212 if [ $n -eq 0 ] 213 then 214 echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile 215 source /etc/profile 216 else 217 source /etc/profile 218 fi 219 220 #7、创建日志目录、/var/run/mysqld/ 221 [ ! -d /var/log/mysql/ ] && mkdir /var/log/mysql/ && chown -R mysql:mysql /var/log/mysql/ 222 [ ! -d /var/run/mysqld/ ] && mkdir /var/run/mysqld/ && chown -R mysql:mysql /var/run/mysqld/ 223 [ ! -d /usr/local/mysql/tmp/ ] && mkdir /usr/local/mysql/tmp/ && chown -R mysql:mysql /usr/local/mysql/tmp/ 224 225 #8、配置成mysqld服务并启动 226 cat > /usr/lib/systemd/system/mysqld.service << EOF 227 [Unit] 228 Description=MySQL Server 229 After=network.target 230 After=syslog.target 231 232 [Install] 233 WantedBy=multi-user.target 234 235 [Service] 236 User=mysql 237 Group=mysql 238 ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf 239 240 #连接数限制 241 LimitNOFILE=65535 242 LimitNPROC=65535 243 244 #Restart配置可以在进程被kill掉之后,让systemctl产生新的进程,避免服务挂掉 245 #Restart=always 246 PrivateTmp=false 247 EOF 248 249 systemctl daemon-reload && systemctl enable mysqld && systemctl start mysqld && systemctl daemon-reload 250 251 if [ $? -eq 0 ] 252 then 253 echo -e "\033[36m==========mysql安装成功并启动==========\033[0m" 254 fi 255 256 n=`grep "/usr/local/mysql/bin" /etc/profile |wc -l` 257 258 if [ $n -eq 0 ] 259 then 260 echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile 261 source /etc/profile 262 else 263 source /etc/profile 264 fi 265 266 mysql -uroot -D mysql -e "UPDATE user SET authentication_string=PASSWORD("123456789") WHERE user='root';" 267 mysql -uroot -e "FLUSH PRIVILEGES;" 268 mysql -uroot -p123456789 -e "grant all privileges on *.* to root@'%' identified by '123456789';" 269 270 if [ $? -eq 0 ] 271 then 272 echo -e "\033[36m==========数据库root密码修改为123456789成功==========\033[0m" 273 else 274 echo -e "\033[31m**********数据库root密码修改为123456789失败,为空**********\033[0m" 275 fi 276 } 277 278 #(3)编译安装php 279 install_php(){ 280 cd /software 281 282 PHPVERS=7.2.6 #PHP版本 283 DLOAD_PHP=http://mirrors.sohu.com/php/php-$PHPVERS.tar.gz #PHP下载地址 284 285 #0 、创建php-fpm用户,用户名:php-fpm 286 id php-fpm &> /dev/null 287 [ $? -ne 0 ] && useradd -r -s /sbin/nologin php-fpm 288 289 #1、下载php 290 [ ! -f php-$PHPVERS.tar.gz ] && echo -e "\033[36m正在下载php源码包...\033[0m" && curl -O $DLOAD_PHP 291 292 #2、解压php 293 [ ! -d php-$PHPVERS/ ] && tar xf php-$PHPVERS.tar.gz 294 echo -e "\033[36m编译安装php需要很长时间,请慢慢等待...\033[0m" 295 sleep 3 296 cd php-$PHPVERS/ 297 298 #3、编译安装 299 ./configure --prefix=/usr/local/php7 \ 300 --with-config-file-path=/usr/local/php7/etc \ 301 --with-mysqli=mysqlnd \ 302 --with-pdo-mysql=mysqlnd \ 303 --with-mysql=/usr/local/mysql \ 304 --with-mysql-sock=/usr/local/mysql/tmp/mysql.sock \ 305 --with-iconv-dir \ 306 --with-freetype-dir \ 307 --with-jpeg-dir \ 308 --with-png-dir \ 309 --with-zlib \ 310 --with-bz2 \ 311 --with-libxml-dir \ 312 --with-curl \ 313 --with-gd \ 314 --with-openssl \ 315 --with-mhash \ 316 --with-xmlrpc \ 317 --with-pdo-mysql \ 318 --with-libmbfl \ 319 --with-onig \ 320 --with-pear \ 321 --enable-xml \ 322 --enable-bcmath \ 323 --enable-shmop \ 324 --enable-sysvsem \ 325 --enable-inline-optimization \ 326 --enable-mbregex \ 327 --enable-fpm \ 328 --enable-mbstring \ 329 --enable-pcntl \ 330 --enable-sockets \ 331 --enable-zip \ 332 --enable-soap \ 333 --enable-opcache \ 334 --enable-pdo \ 335 --enable-mysqlnd-compression-support \ 336 --enable-maintainer-zts \ 337 --enable-session \ 338 --with-fpm-user=php-fpm \ 339 --with-fpm-group=php-fpm && make -j 2 && make -j 2 install 340 341 if [ $? -eq 0 ] 342 then 343 echo -e "\033[36m==========php编译安装成功==========\033[0m" 344 else 345 echo -e "\033[31m**********php编译安装失败,请检查**********\033[0m" 346 exit 1 347 fi 348 349 #4、配置php-fpm服务文件 350 [ ! -d /usr/local/php7/etc/ ] && mkdir /usr/local/php7/etc/ 351 \cp php.ini-production /usr/local/php7/etc/php.ini 352 sed -i '/post_max_size/s/8/16/g;/max_execution_time/s/30/300/g;/max_input_time/s/60/300/g;s#\;date.timezone.*#date.timezone \= Asia/Shanghai#g' /usr/local/php7/etc/php.ini 353 \cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 354 chmod +x /etc/init.d/php-fpm 355 356 #5、将php-fpm添加到系统服务 357 chkconfig --add php-fpm 358 chkconfig php-fpm on 359 360 #6、php-fpm配置文件 361 cd /usr/local/php7/ 362 cat > etc/php-fpm.conf << EOF 363 [global] 364 pid = /usr/local/php7/var/run/php-fpm.pid 365 error_log = /usr/local/php7/var/log/php-fpm.log 366 [www] 367 listen = 127.0.0.1:9000 368 listen.mode = 666 369 listen.owner = nobody 370 listen.group = nobody 371 user = php-fpm 372 group = php-fpm 373 pm = dynamic 374 pm.max_children = 50 375 pm.start_servers = 20 376 pm.min_spare_servers = 5 377 pm.max_spare_servers = 35 378 pm.max_requests = 500 379 rlimit_files = 1024 380 EOF 381 382 #7、编译ldap模块 383 echo -e "\033[34m==========安装ldap模块==========\033[0m" 384 cd /software/php-$PHPVERS/ext/ldap 385 \cp -af /usr/lib64/libldap* /usr/lib/ 386 /usr/local/php7/bin/phpize 387 [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install 388 sed -i '/\;extension=bz2/aextension=ldap.so' /usr/local/php7/etc/php.ini 389 390 #8、编译gettext模块 391 echo -e "\033[34m==========安装gettext模块==========\033[0m" 392 cd /software/php-$PHPVERS/ext/gettext 393 \cp -af /usr/lib64/libldap* /usr/lib/ 394 /usr/local/php7/bin/phpize 395 [ $? -eq 0 ] && ./configure --with-php-config=/usr/local/php7/bin/php-config && make && make install 396 sed -i '/\;extension=bz2/aextension=gettext.so' /usr/local/php7/etc/php.ini 397 398 echo -e "\033[36m==========PHP安装步骤完成==========\033[0m" 399 } 400 401 #(4)整合nginx和php 402 config_lnmp(){ 403 cd /usr/local/nginx 404 405 #1、编辑nginx配置文件 406 cat > conf/nginx.conf << EOF 407 user nobody nobody; 408 worker_processes 1; 409 error_log /data/logs/nginx/error.log crit; 410 pid /run/nginx.pid; 411 worker_rlimit_nofile 51200; 412 413 events { 414 use epoll; 415 worker_connections 1024; 416 } 417 418 http { 419 include mime.types; 420 log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' 421 '\$status \$body_bytes_sent "\$http_referer" ' 422 '"\$http_user_agent" "\$http_x_forwarded_for"'; 423 424 access_log /data/logs/nginx/access.log main; 425 426 server_tokens off; 427 sendfile on; 428 send_timeout 3m; 429 tcp_nopush on; 430 tcp_nodelay on; 431 keepalive_timeout 65; 432 types_hash_max_size 2048; 433 434 client_header_timeout 3m; 435 client_body_timeout 3m; 436 connection_pool_size 256; 437 client_header_buffer_size 1k; 438 large_client_header_buffers 8 4k; 439 request_pool_size 4k; 440 output_buffers 4 32k; 441 postpone_output 1460; 442 client_max_body_size 10m; 443 client_body_buffer_size 256k; 444 client_body_temp_path /usr/local/nginx/client_body_temp; 445 proxy_temp_path /usr/local/nginx/proxy_temp; 446 fastcgi_temp_path /usr/local/nginx/fastcgi_temp; 447 fastcgi_intercept_errors on; 448 449 gzip on; 450 gzip_min_length 1k; 451 gzip_buffers 4 8k; 452 gzip_comp_level 5; 453 gzip_http_version 1.1; 454 gzip_types text/plain application/x-javascript text/css text/htm 455 application/xml; 456 457 default_type application/octet-stream; 458 include /usr/local/nginx/conf.d/*.conf; 459 } 460 EOF 461 #2、将server配置段从nginx.conf分离出来 462 [ ! -d conf.d ] && mkdir conf.d 463 464 #3、创建conf.d/server.conf文件整合nginx与php 465 cat > conf.d/server.conf << EOF 466 server { 467 listen 80; 468 server_name localhost; 469 location / { 470 root /usr/local/nginx/html; 471 index index.php index.html index.htm; 472 } 473 474 error_page 500 502 503 504 /50x.html; 475 location = /50x.html { 476 root html; 477 } 478 479 location ~ \.php$ { 480 root /usr/local/nginx/html; 481 fastcgi_pass 127.0.0.1:9000; 482 fastcgi_index index.php; 483 fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; 484 include fastcgi_params; 485 } 486 } 487 EOF 488 489 #4、修改conf/fastcgi_params 490 > conf/fastcgi_params 491 cat > conf/fastcgi_params << EOF 492 fastcgi_param GATEWAY_INTERFACE CGI/1.1; 493 fastcgi_param SERVER_SOFTWARE nginx; 494 fastcgi_param QUERY_STRING \$query_string; 495 fastcgi_param REQUEST_METHOD \$request_method; 496 fastcgi_param CONTENT_TYPE \$content_type; 497 fastcgi_param CONTENT_LENGTH \$content_length; 498 fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; 499 fastcgi_param SCRIPT_NAME \$fastcgi_script_name; 500 fastcgi_param REQUEST_URI \$request_uri; 501 fastcgi_param DOCUMENT_URI \$document_uri; 502 fastcgi_param DOCUMENT_ROOT \$document_root; 503 fastcgi_param SERVER_PROTOCOL \$server_protocol; 504 fastcgi_param REMOTE_ADDR \$remote_addr; 505 fastcgi_param REMOTE_PORT \$remote_port; 506 fastcgi_param SERVER_ADDR \$server_addr; 507 fastcgi_param SERVER_PORT \$server_port; 508 fastcgi_param SERVER_NAME \$server_name; 509 EOF 510 511 #5、关闭SELinux、防火墙 512 setenforce 0 && sed -i 's/enforcing/disabled/g' /etc/selinux/config 513 systemctl stop firewalld && systemctl disable firewalld 514 515 #6、创建测试页,备份nginx默认的页面 516 517 cat > html/1.php << EOF 518 <?php 519 echo "php解析正常"; 520 ?> 521 EOF 522 523 if [ $? -eq 0 ] 524 then 525 sleep 3 526 echo -e "\033[36m==========整合nginx和php成功==========\033[0m" 527 else 528 echo -e "\033[31m**********整合nginx和php失败,请检查**********\033[0m" 529 # exit 1 530 fi 531 } 532 533 #(5)启动nginx、mysql、php-fpm服务 534 start_service(){ 535 systemctl daemon-reload && systemctl start nginx 536 537 if [ $? -eq 0 ] 538 then 539 echo -e "\033[36m==========nginx服务启动成功==========\033[0m" 540 else 541 echo -e "\033[31m**********nginx服务启动失败**********\033[0m" 542 fi 543 544 systemctl start mysqld && /etc/init.d/php-fpm start 545 546 if [ $? -eq 0 ] 547 then 548 sleep 3 549 echo -e "\033[36m==========mysql、php-fpm服务启动成功==========\033[0m" 550 echo -e "\033[33m查看端口启用情况:\033[0m" 551 ss -tnl 552 echo -e "\033[33m端口:80、3306、9000已启动!\033[0m" 553 echo -e "\033[36m==========编译安装lnmp已完成==========\033[0m" 554 echo -e "\033[36m==========数据库root密码为123456789==========\033[0m" 555 echo -e "\033[36m--------打开浏览器输入你的ip/1.php,看看测试页--------\033[0m" 556 else 557 echo -e "\033[31m**********mysql、php-fpm服务启动失败**********\033[0m" 558 fi 559 560 source /etc/profile 561 } 562 563 #脚本运行入口 564 run_install(){ 565 while true;do 566 menu 567 568 case $num in 569 "0") 570 #0、安装依赖包 571 echo -e "\033[34m==========安装依赖包==========\033[0m" 572 install_package 573 ;; 574 "1") 575 #1、编译安装nginx 576 echo -e "\033[34m==========编译安装nginx==========\033[0m" 577 install_nginx 578 ;; 579 "2") 580 #2、编译安装mysql 581 echo -e "\033[34m==========编译安装mysql==========\033[0m" 582 install_mysql 583 ;; 584 "3") #3、编译安装php 585 echo -e "\033[34m==========编译安装php==========\033[0m" 586 install_php 587 ;; 588 "4") #4、整合nginx和php 589 echo -e "\033[34m==========整合nginx和php==========\033[0m" 590 config_lnmp 591 ;; 592 "5") #5、启动nginx、mysql、php-fpm服务 593 echo -e "\033[34m==========启动nginx、mysql、php-fpm服务==========\033[0m" 594 start_service 595 ;; 596 "6") #6、一键编译安装lnmp 597 echo -e "\033[34m==========一键编译安装并配置lnmp==========\033[0m" 598 install_package 599 install_nginx 600 install_mysql 601 install_php 602 config_lnmp 603 start_service 604 exit 0 605 ;; 606 "7") #7、退出脚本 607 exit 0 608 ;; 609 *) 610 ;; 611 esac 612 done 613 } 614 615 #调用脚本运行入口 616 run_install