首页 > 其他分享 >LNMP环境自动化安装脚本

LNMP环境自动化安装脚本

时间:2022-12-30 16:02:22浏览次数:35  
标签:脚本 enable -- MYSQL LNMP usr Install 自动化 php

一、脚本的环境介绍

此脚本运行在RHEL(CentOS)6.4版本及以上

运行此脚本需注意:

1、主机需要能够上网

2、需了解软件之间的相互依赖性。

二、脚本的介绍

脚本是由函数组成,每一个函数实现一个功能,采用select菜单显示+case

三、脚本的功能介绍

1、支持某台机器单独安装某一个软件,如Nginx、Mysql、PHP

2、支持某台机器一键安装LNMP环境

3、如果想一个一个的安装LNMP架构,请注意安装顺序:Nginx–>Mysql–>PHP

4、会自行的把服务开启,并加入到开机自动启动的服务列表中

LAMP自动化安装脚本代码

  1. #!/bin/bash
  2. #Date:2017-5-20 13:14:00
  3. #Author Blog:
  4. # https://www.yangxingzhen.com
  5. #Author WeChat:
  6. # 微信公众号:小柒博客
  7. #Author mirrors site:
  8. # https://mirrors.yangxingzhen.com
  9. #About the Author
  10. # BY:YangXingZhen
  11. # Mail:[email protected]
  12. #Auto Install LNMP environment

  13. source /etc/rc.d/init.d/functions

  14. #Define Nginx path variables
  15. NGINX_URL=http://nginx.org/download
  16. NGINX_FILE=nginx-1.16.1.tar.gz
  17. NGINX_FILE_DIR=nginx-1.16.1
  18. NGINX_PREFIX=/usr/local/nginx

  19. #Define Boost path variables
  20. Boost_URL=https://mirrors.yangxingzhen.com/mysql
  21. Boost_File=boost_1_59_0.tar.gz

  22. #Define Mysql path variables
  23. MYSQL_URL=http://mirrors.163.com/mysql/Downloads/MySQL-5.7
  24. MYSQL_FILES=mysql-5.7.29.tar.gz
  25. MYSQL_FILES_DIR=mysql-5.7.29
  26. MYSQL_PREFIX=/usr/local/mysql
  27. MYSQL_DIR=/data/mysql
  28. MYSQL_USER=mysql

  29. #Define PHP path variables
  30. PHP_URL=http://mirrors.sohu.com/php
  31. PHP_FILE=php-7.3.7.tar.gz
  32. PHP_FILE_DIR=php-7.3.7
  33. PHP_PREFIX=/usr/local/php
  34. USER=www

  35. #Define ZIP path variables
  36. ZIP_URL=https://nih.at/libzip
  37. ZIP_FILE=libzip-1.2.0.tar.gz
  38. ZIP_FILE_DIR=libzip-1.2.0

  39. function Install_Nginx() {
  40. #Install Nginx Soft
  41. if [ ! -d ${NGINX_PREFIX} ];then
  42. yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ wget
  43. wget -c ${NGINX_URL}/${NGINX_FILE}
  44. tar zxf ${NGINX_FILE}
  45. cd ${NGINX_FILE_DIR}
  46. sed -i 's/1.16.1/ /;s/nginx\//nginx/' src/core/nginx.h
  47. useradd -s /sbin/nologin www
  48. ./configure --prefix=${NGINX_PREFIX} \
  49. --user=www \
  50. --group=www \
  51. --with-http_ssl_module \
  52. --with-http_stub_status_module
  53. if [ $? -eq 0 ];then
  54. make && make install
  55. action "NGINX Install Success..." /bin/true
  56. else
  57. action "NGINX Install Failed..." /bin/false
  58. exit 1
  59. fi
  60. else
  61. echo -e "\033[32m Nginx has been installed \033[0m"
  62. fi
  63. }

  64. function Install_Mysql() {
  65. if [ ! -d ${MYSQL_PREFIX} ];then
  66. #Install Package
  67. yum -y install ncurses-devel perl perl-devel cmake wget gcc gcc-c++ bison* autoconf openssl-devel openssl

  68. #Install Boost
  69. wget -c ${Boost_URL}/${Boost_File}
  70. tar zxf ${Boost_File} -C /usr/local/

  71. #Install MYSQL
  72. wget -c ${MYSQL_URL}/${MYSQL_FILES}
  73. tar zxf ${MYSQL_FILES}
  74. cd ${MYSQL_FILES_DIR}
  75. cmake . -DCMAKE_INSTALL_PREFIX=${MYSQL_PREFIX} \
  76. -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
  77. -DMYSQL_DATADIR=${MYSQL_DIR} \
  78. -DSYSCONFDIR=/etc \
  79. -DEXTRA_CHARSETS=all \
  80. -DDEFAULT_CHARSET=utf8 \
  81. -DDEFAULT_COLLATION=utf8_general_ci \
  82. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  83. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  84. -DWITH_MEMORY_STORAGE_ENGINE=1 \
  85. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  86. -DDOWNLOAD_BOOST=1 \
  87. -DWITH_BOOST=/usr/local/boost_1_59_0 \
  88. -DENABLED_LOCAL_INFILE=1 \
  89. -DMYSQL_TCP_PORT=3306 \
  90. -DWITH_READLINE=1 \
  91. -DMYSQL_USER=${MYSQL_USER} \
  92. -DWITH_SSL=yes
  93. if [ $? -eq 0 ];then
  94. make && make install
  95. action "The MYSQL Install Sussess..." /bin/true
  96. else
  97. action "The MYSQL Install Failed..." /bin/false
  98. exit 1
  99. fi
  100. else
  101. echo -e "\033[31mThe MYSQL already Install...\033[0m"
  102. exit 1
  103. fi
  104. }

  105. function Install_PHP() {
  106. #Install Libzip
  107. yum –y install wget gcc gcc-c++
  108. wget -c ${ZIP_URL}/${ZIP_FILE}
  109. tar zxf ${ZIP_FILE}
  110. cd ${ZIP_FILE_DIR}
  111. ./configure
  112. if [ $? -eq 0 ];then
  113. make && make install
  114. action "The Libzip Install Sussess..." /bin/true
  115. else
  116. action "The Libzip Install Failed..." /bin/false
  117. exit 1
  118. fi

  119. cat >/etc/ld.so.conf <<EOF
  120. /usr/local/lib64
  121. /usr/local/lib
  122. /usr/lib
  123. /usr/lib64
  124. EOF
  125. ldconfig -v

  126. #Install PHP
  127. if [ ! -d ${PHP_PREFIX} ];then
  128. #Install Package
  129. yum -y install epel-release
  130. yum -y install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devellibxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel cmake
  131. cd ~ && wget -c ${PHP_URL}/${PHP_FILE}
  132. tar zxf ${PHP_FILE}
  133. cd ${PHP_FILE_DIR}
  134. ./configure --prefix=${PHP_PREFIX} \
  135. --with-config-file-path=/etc \
  136. --enable-fpm \
  137. --with-fpm-user=${USER} \
  138. --with-fpm-group=${USER} \
  139. --enable-inline-optimization \
  140. --disable-debug \
  141. --disable-rpath \
  142. --enable-shared \
  143. --enable-soap \
  144. --with-libxml-dir \
  145. --with-xmlrpc \
  146. --with-openssl \
  147. --with-mhash \
  148. --with-pcre-regex \
  149. --with-sqlite3 \
  150. --with-zlib \
  151. --enable-bcmath \
  152. --with-iconv \
  153. --with-bz2 \
  154. --enable-calendar \
  155. --with-curl \
  156. --with-cdb \
  157. --enable-dom \
  158. --enable-exif \
  159. --enable-fileinfo \
  160. --enable-filter \
  161. --with-pcre-dir \
  162. --enable-ftp \
  163. --with-gd \
  164. --with-openssl-dir \
  165. --with-jpeg-dir \
  166. --with-png-dir \
  167. --with-zlib-dir \
  168. --with-freetype-dir \
  169. --enable-gd-jis-conv \
  170. --with-gettext \
  171. --with-gmp \
  172. --with-mhash \
  173. --enable-json \
  174. --enable-mbstring \
  175. --enable-mbregex \
  176. --enable-mbregex-backtrack \
  177. --with-onig \
  178. --enable-pdo \
  179. --with-mysqli=mysqlnd \
  180. --with-pdo-mysql=mysqlnd \
  181. --with-zlib-dir \
  182. --with-pdo-sqlite \
  183. --with-readline \
  184. --enable-session \
  185. --enable-shmop \
  186. --enable-simplexml \
  187. --enable-sockets \
  188. --enable-sysvmsg \
  189. --enable-sysvsem \
  190. --enable-sysvshm \
  191. --enable-wddx \
  192. --with-libxml-dir \
  193. --with-xsl \
  194. --enable-zip \
  195. --enable-mysqlnd-compression-support \
  196. --with-pear \
  197. --enable-opcache
  198. if [ $? -eq 0 ];then
  199. \cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
  200. make && make install
  201. action "The PHP Install Sussess..." /bin/true
  202. else
  203. action "The PHP Install Failed..." /bin/false
  204. exit 1
  205. fi
  206. else
  207. echo -e "\033[31mThe PHP already Install...\033[0m"
  208. exit 1
  209. fi
  210. }

  211. function LNMP_Config() {
  212. #Nginx config
  213. useradd -s /sbin/nologin ${USER} >/dev/null 2>&1
  214. ln -sf ${NGINX_PREFIX}/sbin/nginx /usr/sbin
  215. cat >${NGINX_PREFIX}/conf/nginx.conf <<EOF
  216. user ${USER} ${USER};
  217. worker_processes auto;
  218. pid logs/nginx.pid;
  219. events {
  220. use epoll;
  221. worker_connections 10240;
  222. multi_accept on;
  223. }
  224. http {
  225. include mime.types;
  226. default_type application/octet-stream;
  227. log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
  228. '\$status \$body_bytes_sent "\$http_referer" '
  229. '"\$http_user_agent" "\$http_x_forwarded_for"';
  230. access_log logs/access.log main;
  231. error_log logs/error.log warn;
  232. sendfile on;
  233. tcp_nopush on;
  234. keepalive_timeout 120;
  235. tcp_nodelay on;
  236. server_tokens off;
  237. gzip on;
  238. gzip_min_length 1k;
  239. gzip_buffers 4 64k;
  240. gzip_http_version 1.1;
  241. gzip_comp_level 4;
  242. gzip_types text/plain application/x-javascript text/css application/xml;
  243. gzip_vary on;
  244. client_max_body_size 10m;
  245. client_body_buffer_size 128k;
  246. proxy_connect_timeout 90;
  247. proxy_send_timeout 90;
  248. proxy_buffer_size 4k;
  249. proxy_buffers 4 32k;
  250. proxy_busy_buffers_size 64k;
  251. large_client_header_buffers 4 4k;
  252. client_header_buffer_size 4k;
  253. open_file_cache_valid 30s;
  254. open_file_cache_min_uses 1;
  255. server {
  256. listen 80;
  257. server_name localhost;
  258. location / {
  259. root html;
  260. index index.html index.htm;
  261. }
  262. location ~ \.php$ {
  263. fastcgi_pass 127.0.0.1:9000;
  264. fastcgi_index index.php;
  265. fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  266. include fastcgi_params;
  267. }
  268. }
  269. }
  270. EOF

  271. #Create a PHP test page
  272. cat >${NGINX_PREFIX}/html/index.php <<EOF
  273. <?php
  274. phpinfo();
  275. ?>
  276. EOF

  277. #Config PHP
  278. \cp php.ini-production /etc/php.ini
  279. \cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  280. \cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
  281. \cp sapi/fpm/php-fpm.service /usr/lib/systemd/system
  282. cat >/usr/local/php/etc/php-fpm.d/www.conf <<EOF
  283. [www]
  284. listen = 0.0.0.0:9000
  285. listen.mode = 0666
  286. user = www
  287. group = www
  288. pm = dynamic
  289. pm.max_children = 128
  290. pm.start_servers = 20
  291. pm.min_spare_servers = 5
  292. pm.max_spare_servers = 35
  293. pm.max_requests = 10000
  294. rlimit_files = 1024
  295. slowlog = log/$pool.log.slow
  296. EOF

  297. #Config MYSQL
  298. #Mysql config
  299. useradd -s /sbin/nlogin mysql >/dev/null 2>&1
  300. mkdir -p ${MYSQL_DIR}
  301. chown -R ${MYSQL_USER}.${MYSQL_USER} ${MYSQL_DIR}
  302. cat >/etc/my.cnf <<EOF
  303. [mysqld]
  304. #数据存储目录
  305. datadir = ${MYSQL_DIR}
  306. #socket通信文件
  307. socket = /tmp/mysql.sock
  308. #使用mysql用户启动
  309. user = ${MYSQL_USER}
  310. #MYSQL服务运行的端口号
  311. port = 3306
  312. #开启bin-log日志
  313. log-bin = mysql-bin
  314. #MYSQL服务ID号
  315. server-id = 1
  316. #定义error错误文件
  317. log-error = ${MYSQL_DIR}/mysqld.log
  318. #PID文件路径
  319. pid-file = mysqld.pid
  320. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  321. #设置字符集为utf8
  322. character-set-server = utf8
  323. [client]
  324. default-character-set = utf8
  325. port = 3306
  326. socket = /tmp/mysql.sock
  327. [mysql]
  328. default-character-set = utf8
  329. EOF

  330. #Initialization Mysql
  331. /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/mysql/ --basedir=/usr/local/mysql
  332. ln -sf ${MYSQL_PREFIX}/bin/* /usr/bin
  333. \cp ${MYSQL_PREFIX}/support-files/mysql.server /etc/init.d/mysqld
  334. chmod o+x /etc/init.d/mysqld

  335. #Config iptables(firewalld)、selinux
  336. if [ ${SYSTEM_VERSION} -eq 6 ];then
  337. service iptables stop
  338. chkconfig iptables off
  339. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  340. setenforce 0
  341. else
  342. systemctl stop firewalld.service
  343. systemctl disable firewalld.service
  344. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  345. setenforce 0
  346. fi

  347. #Start MYSQL、php-fpm、nginx and Add MySQL、php-fpm、nginx boot self start
  348. ${NGINX_PREFIX}/sbin/nginx
  349. systemctl start php-fpm
  350. /etc/init.d/mysqld start
  351. grep -qw "${NGINX_PREFIX}" /etc/rc.d/rc.local
  352. if [ $? -ne 0 ];then
  353. echo "${NGINX_PREFIX}/sbin/nginx" >>/etc/rc.d/rc.local
  354. chmod +x /etc/rc.d/rc.local
  355. fi
  356. systemctl enable php-fpm
  357. chkconfig --add mysqld
  358. chkconfig mysqld on
  359. chmod +x /etc/rc.d/rc.local
  360. }

  361. function select_port_process() {
  362. #Select ports and processes
  363. netstat -lntup |egrep "80|9000|3306"
  364. ps -ef | grep -v grep |egrep "nginx|php-fpm|mysql"
  365. }

  366. PS3="Please enter you select install menu:"
  367. select i in LNMP Install_Nginx Install_Mysql Install_PHP LNMP_Config select_port_process quit
  368. do
  369. case $i in
  370. LNMP)
  371. Install_Nginx
  372. Install_Mysql
  373. Install_PHP
  374. LNMP_Config
  375. ;;
  376. Install_Nginx)
  377. Install_Nginx
  378. ;;
  379. Install_Mysql)
  380. Install_Mysql
  381. ;;
  382. Install_PHP)
  383. Install_PHP
  384. ;;
  385. LNMP_Config)
  386. LNMP_Config
  387. ;;
  388. select_port_process)
  389. select_port_process
  390. ;;
  391. quit)
  392. echo -e "\033[32m Exit selection Menu.\033[0m"
  393. exit 1
  394. esac
  395. done

保存,上传至服务器即可使用

  • 输入编号:6677,直达文章
  • 输入m|M,直达目录列表

标签:脚本,enable,--,MYSQL,LNMP,usr,Install,自动化,php
From: https://blog.51cto.com/u_12018693/5980588

相关文章