LTMP手动编译安装以及全自动化部署实践(附详细代码)

系统 Linux 系统运维 自动化
大家使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(Nginx/MySQL/PHP/Apache),LAMP(Apache/MySQL/PHP)等类似的开发或生产环境。本文较为详细的描述了基于LTMP架构的部署过程。

  [[144364]]

一、前言

  现在很多朋友都了解或者已经在使用LNMP架构,一般可以理解为Linux Shell为CentOS/RadHat/Fedora/Debian/Ubuntu/等平台安装LNMP(Nginx/MySQL /PHP),LNMPA(Nginx/MySQL/PHP/Apache),LAMP(Apache/MySQL/PHP)等类似的开发或生产环境。我自己是从SuSE/Oracle商业化环境走出来的,对于开源的部署方案也是在一点一点摸索,我相信其中也必然包含某些坑爹的配置。这篇文章较为详细的描述了基于LTMP架构的部署过程,之后会再考虑独立各个模块分享细节和技巧,如果大家有更合适的配置实践手册欢迎一起分享,文章中的错误和改进点也请帮忙指点下哈。

  LTMP(CentOS/Tengine/MySQL/PHP)

  二、LTMP版本

  1. CentOS_6.5_64
  2. Tengine-2.1.0
  3. MySQL_5.6.25
  4. PHP_5.5.27
  5. Apache_2.2.31(酱油)

  三、准备工作

  如果允许公网访问会方便很多

  1. #优化History历史记录 
  2. vi /etc/bashrc 
  3.  
  4. #设置保存历史命令的文件大小 
  5. export HISTFILESIZE=1000000000 
  6. #保存历史命令条数 
  7. export HISTSIZE=1000000 
  8. #实时记录历史命令,默认只有在用户退出之后才会统一记录,很容易造成多个用户间的相互覆盖。 
  9. export PROMPT_COMMAND="history -a" 
  10. #记录每条历史命令的执行时间 
  11. export HISTTIMEFORMAT="%Y-%m-%d_%H:%M:%S " 
  12.  
  13. #设置时区(可选) 
  14. rm -rf /etc/localtime 
  15. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
  16.  
  17. #禁用NetworkManager(可选) 
  18. /etc/init.d/NetworkManager stop 
  19. chkconfig NetworkManager off 
  20. /etc/init.d/network restart 
  21.  
  22. #关闭iptables(可选)  
  23. /etc/init.d/iptables stop 
  24. chkconfig iptables off 
  25.  
  26. #设置dns(可选) 
  27. echo "nameserver 114.114.114.114" > /etc/resolv.conf  
  28.  
  29. #关闭maildrop 
  30. #cd /var/spool/postfix/maildrop;ls | xargs rm -rf;  
  31. sed 's/MAILTO=root/MAILTO=""/g' /etc/crontab 
  32. service crond restart 
  33.  
  34. #关闭selinux 
  35. setenforce 0 
  36. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config   
  37.  
  38.  
  39. #文件打开数量, 
  40. echo ulimit -SHn 65535 >> /etc/profile 
  41. source /etc/profile 
  42.  
  43. #修改最大进程和最大文件打开数限制 
  44. vi /etc/security/limits.conf 
  45. * soft nproc 11000 
  46. * hard nproc 11000 
  47. * soft nofile 655350 
  48. * hard nofile 655350 
  49.  
  50. sed -i -e '/# End of file/i\* soft  nofile 65535\n* hard nofile 65535'  /etc/security/limits.conf 
  51.  
  52. #优化TCP 
  53. vi /etc/sysctl.conf 
  54.  
  55. net.ipv4.ip_forward = 0   
  56. net.ipv4.conf.default.rp_filter = 1   
  57. net.ipv4.conf.default.accept_source_route = 0   
  58. kernel.sysrq = 0   
  59. kernel.core_uses_pid = 1   
  60. #开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies来处理 
  61. net.ipv4.tcp_syncookies = 1   
  62. kernel.msgmnb = 65536   
  63. kernel.msgmax = 65536   
  64. kernel.shmmax = 68719476736   
  65. kernel.shmall = 4294967296   
  66. #timewait的数量,默认是180000 
  67. net.ipv4.tcp_max_tw_buckets = 6000   
  68. net.ipv4.tcp_sack = 1   
  69. net.ipv4.tcp_window_scaling = 1   
  70. net.ipv4.tcp_rmem = 4096 87380 4194304    
  71. net.ipv4.tcp_wmem = 4096 16384 4194304    
  72. net.core.wmem_default = 8388608   
  73. net.core.rmem_default = 8388608   
  74. net.core.rmem_max = 16777216   
  75. net.core.wmem_max = 16777216   
  76. #每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目 
  77. net.core.netdev_max_backlog = 262144   
  78. #web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值 
  79. net.core.somaxconn = 262144   
  80. #系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS攻击,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)      
  81. net.ipv4.tcp_max_orphans = 3276800   
  82. #记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M内存的系统而言,缺省值是1024,小内存的系统则是128 
  83. net.ipv4.tcp_max_syn_backlog = 262144   
  84. #时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包。这里需要将其关掉 
  85. net.ipv4.tcp_timestamps = 0   
  86. #为了打开对端的连接,内核需要发送一个SYN并附带一个回应前面一个SYN的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK包的数量 
  87. net.ipv4.tcp_synack_retries = 1   
  88. #在内核放弃建立连接之前发送SYN包的数量 
  89. net.ipv4.tcp_syn_retries = 1   
  90. #启用timewait快速回收      
  91. net.ipv4.tcp_tw_recycle = 1   
  92. #开启重用,允许将TIME-WAIT sockets重新用于新的TCP连接 
  93. net.ipv4.tcp_tw_reuse = 1   
  94. net.ipv4.tcp_mem = 94500000 915000000 927000000    
  95. #如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是60 秒。2.2 内核的通常值是180秒,你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2的危险性比FIN-WAIT-1要小,因为它最多只能吃掉1.5K内存,但是它们的生存期长些。 
  96. net.ipv4.tcp_fin_timeout = 1   
  97. #当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。 
  98. net.ipv4.tcp_keepalive_time = 30   
  99. #允许系统打开的端口范围 
  100. net.ipv4.ip_local_port_range = 1024 65000  
  101. #表示文件句柄的最大数量 
  102. fs.file-max = 102400 
  103.  
  104. #云主机上的优化 
  105.  
  106. # Kernel sysctl configuration file for Red Hat Linux 
  107. For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and 
  108. # sysctl.conf(5) for more details. 
  109.  
  110. # Controls IP packet forwarding 
  111. net.ipv4.ip_forward = 0 
  112.  
  113. # Controls source route verification 
  114. net.ipv4.conf.default.rp_filter = 1 
  115.  
  116. # Do not accept source routing 
  117. net.ipv4.conf.default.accept_source_route = 0 
  118.  
  119. # Controls the System Request debugging functionality of the kernel 
  120.  
  121. # Controls whether core dumps will append the PID to the core filename. 
  122. # Useful for debugging multi-threaded applications. 
  123. kernel.core_uses_pid = 1 
  124.  
  125. # Controls the use of TCP syncookies 
  126. net.ipv4.tcp_syncookies = 1 
  127.  
  128. # Disable netfilter on bridges. 
  129. net.bridge.bridge-nf-call-ip6tables = 0 
  130. net.bridge.bridge-nf-call-iptables = 0 
  131. net.bridge.bridge-nf-call-arptables = 0 
  132.  
  133. # Controls the default maxmimum size of a mesage queue 
  134. kernel.msgmnb = 65536 
  135.  
  136. # Controls the maximum size of a message, in bytes 
  137. kernel.msgmax = 65536 
  138.  
  139. # Controls the maximum shared segment sizein bytes 
  140. kernel.shmmax = 68719476736 
  141.  
  142. # Controls the maximum number of shared memory segments, in pages 
  143. kernel.shmall = 4294967296 
  144. net.ipv4.conf.all.send_redirects = 0 
  145. net.ipv4.conf.default.send_redirects = 0 
  146. net.ipv4.conf.all.secure_redirects = 0 
  147. net.ipv4.conf.default.secure_redirects = 0 
  148. net.ipv4.conf.all.accept_redirects = 0 
  149. net.ipv4.conf.default.accept_redirects = 0 
  150. net.ipv4.conf.all.send_redirects = 0 
  151. net.ipv4.conf.default.send_redirects = 0 
  152. net.ipv4.conf.all.secure_redirects = 0 
  153. net.ipv4.conf.default.secure_redirects = 0 
  154. net.ipv4.conf.all.accept_redirects = 0 
  155. net.ipv4.conf.default.accept_redirects = 0 
  156. net.netfilter.nf_conntrack_max = 1000000 
  157. kernel.unknown_nmi_panic = 0 
  158. kernel.sysrq = 0 
  159. fs.file-max = 1000000 
  160. vm.swappiness = 10 
  161. fs.inotify.max_user_watches = 10000000 
  162. net.core.wmem_max = 327679 
  163. net.core.rmem_max = 327679 
  164. net.ipv4.conf.all.send_redirects = 0 
  165. net.ipv4.conf.default.send_redirects = 0 
  166. net.ipv4.conf.all.secure_redirects = 0 
  167. net.ipv4.conf.default.secure_redirects = 0 
  168. net.ipv4.conf.all.accept_redirects = 0 
  169. net.ipv4.conf.default.accept_redirects = 0 
  170.  
  171. /sbin/sysctl -p 
  172.  
  173. #自动选择最快的yum源 
  174. yum -y install yum-fastestmirror 
  175.  
  176. #移除系统自带的rpm包的http mysql php 
  177. #yum remove httpd* php* 
  178. yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y 
  179.  
  180. #升级基础库 
  181. yum install -y wget gcc gcc-c++ openssl* curl curl-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel gd gd2 gd-devel gd2-devel libaio autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel  
  182.  
  183. #yum安装基础必备环境包,可以先将yum源更换为阿里云的源 
  184. 阿里:http://mirrors.aliyun.com/  
  185. 搜狐:http://mirrors.sohu.com/  
  186. 网易:http://mirrors.163.com/ 
  187.  
  188. #备份原先的yum源信息 
  189. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
  190.  
  191. #从阿里云镜像站下载centos6的repo 
  192. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 
  193.  
  194. #最后yum重新生成缓存 
  195. yum makecache 
  196.  
  197. #yum安装软件包(可选) 
  198. yum -y install tar zip unzip openssl* gd gd-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers make libmcrypt libmcrypt-devel fontconfig fontconfig-devel libXpm* libtool* libxml2 libxml2-devel t1lib t1lib-devel 
  199.  
  200.  
  201.  
  202. #定义目录结构,下载安装包 
  203. mkdir -p /app/{local,data} 
  204. cd /app/local 
  205.  
  206. #PCRE - Perl Compatible Regular Expressions 
  207. wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz" 
  208. #Tengine 
  209. wget "http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" 
  210. #MySQL 
  211. wget "https://downloads.mariadb.com/archives/mysql-5.6/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz" 
  212. #PHP 
  213. wget "http://cn2.php.net/distributions/php-5.6.11.tar.gz" 
  214. #Mhash 
  215. wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz" 
  216. #libmcrypt 
  217. wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz" 
  218. #Mcrypt 
  219. wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz" 

  #p#

四、配置Tengine

  1.安装PCRE

  1. tar zxvf pcre-8.37.tar.gz 
  2. cd pcre-8.37 
  3. ./configure 
  4. make && make install 
  5. cd ../ 

  2.安装Tengine

  1. #添加www用户和组 
  2. groupadd www 
  3. useradd -g www www 
  4. #安装Tengine 
  5. tar zxvf tengine-2.1.0.tar.gz 
  6. cd tengine-2.1.0 
  7.  
  8. ./configure --user=www --group=www \ 
  9. --prefix=/app/local/nginx \ 
  10. --with-http_stub_status_module \ 
  11. --with-http_ssl_module \ 
  12. --with-pcre=/app/local/pcre-8.37 
  13.  
  14. make && make install 
  15. cd ../ 

  3.配置Nginx

  Nginx配置文件的优化很重要,理解每一步的意义。

  1. #修改nginx.conf 
  2. vi /app/local/nginx/conf/nginx.conf 
  3.  
  4. #用户和用户组 
  5. user  www www; 
  6. #工作进程,一般可以按CPU核数设定 
  7. worker_processes  auto; 
  8. worker_cpu_affinity auto; 
  9. #全局错误日志级别 
  10. # [ debug | info | notice | warn | error | crit ] 
  11. error_log  logs/error.log  error; 
  12. #PID文件位置 
  13. pid  logs/nginx.pid; 
  14. #更改worker进程的最大打开文件数限制,避免"too many open files" 
  15. worker_rlimit_nofile 65535; 
  16.  
  17. #events事件指令是设定Nginx的工作模式及连接数上限 
  18. events{ 
  19.      #epoll是Linux首选的高效工作模式 
  20.      use epoll; 
  21.      #告诉nginx收到一个新连接通知后接受尽可能多的连接 
  22.      multi_accept on
  23.      #用于定义Nginx每个进程的最大连接数 
  24.      worker_connections      65536; 
  25.  
  26. #HTTP模块控制着nginx http处理的所有核心特性 
  27. http { 
  28.     include       mime.types; 
  29.     #设置文件使用的默认的MIME-type 
  30.     default_type  application/octet-stream; 
  31.      
  32.  
  33.     #对日志格式的设定,main为日志格式别名 
  34.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  35.                       '$status $body_bytes_sent "$http_referer" ' 
  36.                       '"$http_user_agent" "$http_x_forwarded_for"'
  37.     #设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快 
  38.     access_log off
  39.     # access_log logs/access.log main buffer=16k; 
  40.  
  41.     #开启gzip压缩,实时压缩输出数据流 
  42.     gzip on
  43.     #设置IE6或者更低版本禁用gzip功能 
  44.     gzip_disable "MSIE [1-6]\."
  45.     #前端的缓存服务器缓存经过gzip压缩的页面 
  46.     gzip_vary on
  47.     #允许压缩基于请求和响应的响应流 
  48.     gzip_proxied any
  49.     #设置数据的压缩等级 
  50.     gzip_comp_level 4; 
  51.     #设置对数据启用压缩的最少字节数 
  52.     gzip_min_length 1k; 
  53.     #表示申请16个单位为64K的内存作为压缩结果流缓存 
  54.     gzip_buffers 16 64k; 
  55.     #用于设置识别HTTP协议版本 
  56.     gzip_http_version 1.1; 
  57.     #用来指定压缩的类型 
  58.     gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 
  59.       
  60.  
  61.     #打开缓存的同时也指定了缓存最大数目,以及缓存的时间 
  62.     open_file_cache max=200000 inactive=20s; 
  63.     #在open_file_cache中指定检测正确信息的间隔时间 
  64.     open_file_cache_valid 30s; 
  65.     #定义了open_file_cache中指令参数不活动时间期间里最小的文件数 
  66.     open_file_cache_min_uses 2; 
  67.     #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件 
  68.     open_file_cache_errors on;  
  69.   
  70.     #设置允许客户端请求的最大的单个文件字节数 
  71.     client_max_body_size 30M; 
  72.     #设置客户端请求主体读取超时时间 
  73.     client_body_timeout 10; 
  74.     #设置客户端请求头读取超时时间 
  75.     client_header_timeout 10; 
  76.     #指定来自客户端请求头的headerbuffer大小 
  77.     client_header_buffer_size 32k; 
  78.     #设置客户端连接保持活动的超时时间 
  79.     keepalive_timeout 60; 
  80.     #关闭不响应的客户端连接 
  81.     reset_timedout_connection on
  82.     #设置响应客户端的超时时间 
  83.     send_timeout 10; 
  84.     #开启高效文件传输模式 
  85.     sendfile on
  86.     #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送 
  87.     tcp_nopush on
  88.     #告诉nginx不要缓存数据,而是一段一段的发送 
  89.     tcp_nodelay on
  90.     #设置用于保存各种key(比如当前连接数)的共享内存的参数 
  91.     limit_conn_zone $binary_remote_addr zone=addr:5m;  
  92.     #给定的key设置最大连接数,允许每一个IP地址最多同时打开有100个连接 
  93.     limit_conn addr 100;  
  94.   
  95.     #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度 
  96.     fastcgi_buffers 256 16k; 
  97.     fastcgi_buffer_size 128k; 
  98.     fastcgi_connect_timeout 3s; 
  99.     fastcgi_send_timeout 120s; 
  100.     fastcgi_read_timeout 120s; 
  101.     server_names_hash_bucket_size 128; 
  102.     #不在error_log中记录不存在的错误 
  103.     log_not_found off
  104.     #关闭在错误页面中的nginx版本数字,提高安全性 
  105.     #server_tag Apache; 
  106.     server_tokens off
  107.     #tengine 
  108.     server_tag off
  109.     server_info off
  110.  
  111.     #添加虚拟主机的配置文件 
  112.     include vhosts/*.conf; 
  113.  
  114.     #负载均衡配置(暂时略过) 
  115.     #upstream test.com 
  116.  
  117.     #设定虚拟主机配置 
  118.     server { 
  119.         #侦听80端口 
  120.         listen       80; 
  121.         #定义使用localhost访问 
  122.         server_name  localhost; 
  123.         #定义首页索引文件的名称 
  124.         index index.html index.htm index.php; 
  125.         #定义服务器的默认网站根目录位置 
  126.         root    /app/data/localhost/; 
  127.   
  128.         #定义错误提示页面 
  129.         error_page  404              /404.html; 
  130.         error_page  500 502 503 504  /50x.html; 
  131.         location = /50x.html { 
  132.             root   html; 
  133.         } 
  134.   
  135.         #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.      
  136.         location ~ .*\.(php|php5)?$ { 
  137.             fastcgi_pass   127.0.0.1:9000; 
  138.             fastcgi_index  index.php; 
  139.             include        fastcgi.conf; 
  140.         } 
  141.          
  142.         #静态文件 
  143.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ 
  144.         { 
  145.             #过期30天,频繁更新可设置小一点 
  146.             expires      30d; 
  147.         } 
  148.   
  149.         location ~ .*\.(js|css)?$ 
  150.         { 
  151.             #过期1小时,不更新可设置大一些 
  152.             expires      1h; 
  153.         } 
  154.         #禁止访问 
  155.         location ~ /\. { 
  156.             deny all
  157.         } 
  158.     } 

 

#p#

简化配置文件

  vi /app/local/nginx/conf/nginx.conf

  1. user  www www; 
  2. worker_processes auto; 
  3. worker_cpu_affinity auto; 
  4.  
  5. error_log  logs/error.log  crit; 
  6. pid        logs/nginx.pid; 
  7.  
  8. worker_rlimit_nofile 51200; 
  9. events 
  10.     use epoll; 
  11.     multi_accept on
  12.     worker_connections 51200; 
  13.  
  14. http 
  15.     include       mime.types; 
  16.     default_type  application/octet-stream; 
  17.  
  18.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  19.                       '$status $body_bytes_sent "$http_referer" ' 
  20.                       '"$http_user_agent" "$http_x_forwarded_for"'
  21.  
  22.     access_log off
  23.     #access_log logs/access.log main buffer=16k; 
  24.  
  25.     server_names_hash_bucket_size 128; 
  26.     client_header_buffer_size 32k; 
  27.     large_client_header_buffers 4 32k; 
  28.     client_max_body_size 50M;  
  29.  
  30.     sendfile on
  31.     tcp_nopush on
  32.     tcp_nodelay on
  33.     keepalive_timeout 60;  
  34.     server_tokens off
  35.     server_tag off
  36.     server_info off
  37.  
  38.     fastcgi_connect_timeout 300; 
  39.     fastcgi_send_timeout 300; 
  40.     fastcgi_read_timeout 300; 
  41.     fastcgi_buffer_size 64k; 
  42.     fastcgi_buffers 4 64k; 
  43.     fastcgi_busy_buffers_size 128k; 
  44.     fastcgi_temp_file_write_size 256k; 
  45.  
  46.     #gzip on
  47.     #gzip_min_length  1k; 
  48.     #gzip_buffers     4 16k; 
  49.     #gzip_http_version 1.1; 
  50.     #gzip_comp_level 5; 
  51.     #gzip_types       text/plain application/x-javascript text/css application/xml; 
  52.     #gzip_vary on
  53.  
  54.     include vhosts/*.conf; 

  分离server写入vhosts

  mkdir -p /app/local/nginx/conf/vhosts/

  vi /app/local/nginx/conf/vhosts/localhost.conf

  1. server { 
  2.     listen       80; 
  3.     server_name  localhost; 
  4.     index index.php index.html index.htm; 
  5.     access_log  logs/localhost.log  main; 
  6.  
  7.     root    /app/data/localhost/; 
  8.  
  9.     location / { 
  10.         index  index.php index.html index.htm; 
  11.     } 
  12.  
  13.     #error_page  404              /404.html; 
  14.     #error_page  500 502 503 504  /50x.html; 
  15.  
  16.     location = /50x.html { 
  17.         root   html; 
  18.     } 
  19.  
  20.     location ~ .*\.(php|php5)?$ { 
  21.         fastcgi_pass   127.0.0.1:9000; 
  22.         fastcgi_index  index.php; 
  23.       #fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name; 
  24.         include        fastcgi.conf; 
  25.     } 
  26.  
  27.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ 
  28.     { 
  29.         expires      30d; 
  30.     } 
  31.  
  32.     location ~ .*\.(js|css)?$ 
  33.     { 
  34.         expires      1h; 
  35.     } 
  36.  
  37.     location ~ /\. { 
  38.         deny all
  39.     } 
  1. #检查语法 
  2. /app/local/nginx/sbin/nginx -t 
  3. # ./nginx -t 
  4. the configuration file /app/local/nginx/conf/nginx.conf syntax is ok 
  5. configuration file /app/local/nginx/conf/nginx.conf test is successful 
  6.  
  7. #测试用例 
  8. mkdir -p /app/data/localhost 
  9. chmod +w /app/data/localhost 
  10. echo "<?php phpinfo();?>" > /app/data/localhost/phpinfo.php 
  11. chown -R www:www /app/data/localhost 
  12.  
  13. #设置nginx系统变量 
  14. echo 'export PATH=$PATH:/app/local/nginx/sbin'>>/etc/profile && source /etc/profile 
  15.  
  16. #测试访问 
  17. curl -I http://localhost 
  18.  
  19. HTTP/1.1 200 OK 
  20. Server: Tengine/2.1.0 
  21. Date: Mon, 27 Jul 2015 06:42:25 GMT 
  22. Content-Type: text/html; charset=UTF-8 
  23. Connection: keep-alive 
  24. X-Powered-By: PHP/5.6.11 

  4.添加Tengine到服务

  配置服务后便于统一管理

  vi /etc/rc.d/init.d/nginx

  1. #!/bin/sh 
  2.   
  3. # Source function library. 
  4. . /etc/rc.d/init.d/functions 
  5.   
  6. # Source networking configuration. 
  7. . /etc/sysconfig/network 
  8.   
  9. Check that networking is up. 
  10. "$NETWORKING" = "no" ] && exit 0 
  11.   
  12. nginx="/app/local/nginx/sbin/nginx" 
  13. prog=$(basename $nginx) 
  14.   
  15. NGINX_CONF_FILE="/app/local/nginx/conf/nginx.conf" 
  16.   
  17. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  18.   
  19. lockfile=/var/lock/subsys/nginx 
  20.   
  21. make_dirs() { 
  22.    # make required directories 
  23.    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  24.    if [ -z "`grep $user /etc/passwd`" ]; then 
  25.        useradd -M -s /bin/nologin $user 
  26.    fi 
  27.    options=`$nginx -V 2>&1 | grep 'configure arguments:'
  28.    for opt in $options; do 
  29.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  30.            value=`echo $opt | cut -d "=" -f 2` 
  31.            if [ ! -d "$value" ]; then 
  32.                # echo "creating" $value 
  33.                mkdir -p $value && chown -R $user $value 
  34.            fi 
  35.        fi 
  36.    done 
  37.   
  38. start() { 
  39.     [ -x $nginx ] || exit 5 
  40.     [ -f $NGINX_CONF_FILE ] || exit 6 
  41.     make_dirs 
  42.     echo -n $"Starting $prog: " 
  43.     daemon $nginx -c $NGINX_CONF_FILE 
  44.     retval=$? 
  45.     echo 
  46.     [ $retval -eq 0 ] && touch $lockfile 
  47.     return $retval 
  48.   
  49. stop() { 
  50.     echo -n $"Stopping $prog: " 
  51.     killproc $prog -QUIT 
  52.     retval=$? 
  53.     echo 
  54.     [ $retval -eq 0 ] && rm -f $lockfile 
  55.     return $retval 
  56.   
  57. restart() { 
  58.     configtest || return $? 
  59.     stop 
  60.     sleep 1 
  61.     start 
  62.   
  63. reload() { 
  64.     configtest || return $? 
  65.     echo -n $"Reloading $prog: " 
  66.     killproc $nginx -HUP 
  67.     RETVAL=$? 
  68.     echo 
  69.   
  70. force_reload() { 
  71.     restart 
  72.   
  73. configtest() { 
  74.   $nginx -t -c $NGINX_CONF_FILE 
  75.   
  76. rh_status() { 
  77.     status $prog 
  78.   
  79. rh_status_q() { 
  80.     rh_status >/dev/null 2>&1 
  81.   
  82. case "$1" in 
  83.     start) 
  84.         rh_status_q && exit 0 
  85.         $1 
  86.         ;; 
  87.     stop) 
  88.         rh_status_q || exit 0 
  89.         $1 
  90.         ;; 
  91.     restart|configtest) 
  92.         $1 
  93.         ;; 
  94.     reload) 
  95.         rh_status_q || exit 7 
  96.         $1 
  97.         ;; 
  98.     force-reload) 
  99.         force_reload 
  100.         ;; 
  101.     status) 
  102.         rh_status 
  103.         ;; 
  104.     condrestart|try-restart) 
  105.         rh_status_q || exit 0 
  106.             ;; 
  107.     *) 
  108.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  109.         exit 2 
  110. esac 

  #p#

五、安装MySQL

  注意目录和字符集等配置文件

  1. #解压mysql 
  2. mkdir -p /app/local/mysql 
  3. tar zxvf mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz 
  4. mv mysql-5.6.25-linux-glibc2.5-x86_64/* /app/local/mysql 
  5. #增加mysql用户组 
  6. groupadd mysql 
  7. useradd -g mysql mysql 
  8. mkdir -p /app/data/mysql/data/ 
  9. mkdir -p /app/data/mysql/binlog/ 
  10. mkdir -p /app/data/mysql/relaylog/ 
  11. chown -R mysql:mysql /app/data/mysql/ 
  12.  #安装mysql 
  13. /app/local/mysql/scripts/mysql_install_db --basedir=/app/local/mysql --datadir=/app/data/mysql/data --user=mysql 
  14. #修改mysqld_safe配置路径 
  15. sed -i "s#/usr/local/mysql#/app/local/mysql#g" /app/local/mysql/bin/mysqld_safe 
  1. #修改my.cnf配置文件 
  2. vi /app/local/mysql/my.cnf 
  3.  
  4. [client] 
  5. character-set-server = utf8 
  6. port = 3306 
  7. socket = /tmp/mysql.sock 
  8.  
  9. [mysql] 
  10. #prompt="(\u:HOSTNAME:)[\d]> " 
  11. prompt="\u@\h \R:\m:\s [\d]> " 
  12. no-auto-rehash 
  13.  
  14. [mysqld] 
  15. server-id = 1 
  16. port = 3306 
  17. user = mysql 
  18. basedir = /app/local/mysql 
  19. datadir = /app/data/mysql/data 
  20. socket = /tmp/mysql.sock 
  21. log-error = /app/data/mysql/mysql_error.log 
  22. pid-file = /app/data/mysql/mysql.pid 
  23. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
  24.  
  25. default-storage-engine = InnoDB 
  26. max_connections = 512 
  27. max_connect_errors = 100000 
  28. table_open_cache = 512 
  29. external-locking = FALSE 
  30. max_allowed_packet = 32M 
  31. slow_query_log = 1 
  32. slow_query_log_file = /app/data/mysql/slow.log 
  33.  
  34. open_files_limit = 10240 
  35. back_log = 600 
  36. join_buffer_size = 2M 
  37. read_rnd_buffer_size = 16M 
  38. sort_buffer_size = 2M 
  39. thread_cache_size = 300 
  40. query_cache_size = 128M 
  41. query_cache_limit = 2M 
  42. query_cache_min_res_unit = 2k 
  43. thread_stack = 192K 
  44. transaction_isolation = READ-COMMITTED 
  45. tmp_table_size = 246M 
  46. max_heap_table_size = 246M 
  47. long_query_time = 3 
  48. log-slave-updates 
  49. log-bin = /app/data/mysql/binlog/binlog 
  50. sync_binlog = 1 
  51. binlog_cache_size = 4M 
  52. binlog_format = MIXED 
  53. max_binlog_cache_size = 8M 
  54. max_binlog_size = 1G 
  55. relay-log-index = /app/data/mysql/relaylog/relaylog 
  56. relay-log-info-file = /app/data/mysql/relaylog/relaylog 
  57. relay-log = /app/data/mysql/relaylog/relaylog 
  58. expire_logs_days = 7 
  59. key_buffer_size = 128M 
  60. read_buffer_size = 1M 
  61. read_rnd_buffer_size = 16M 
  62. bulk_insert_buffer_size = 64M 
  63. myisam_sort_buffer_size = 128M 
  64. myisam_max_sort_file_size = 10G 
  65. myisam_repair_threads = 1 
  66. myisam_recover 
  67.  
  68. innodb_additional_mem_pool_size = 16M 
  69. innodb_buffer_pool_size = 256M 
  70. innodb_data_file_path = ibdata1:1024M:autoextend 
  71. innodb_flush_log_at_trx_commit = 1 
  72. innodb_log_buffer_size = 16M 
  73. innodb_log_file_size = 256M 
  74. innodb_log_files_in_group = 2 
  75. innodb_max_dirty_pages_pct = 50 
  76. innodb_file_per_table = 1 
  77. innodb_locks_unsafe_for_binlog = 0 
  78.  
  79. interactive_timeout = 120 
  80. wait_timeout = 120 
  81.   
  82. skip-name-resolve 
  83. slave-skip-errors = 1032,1062,126,1114,1146,1048,1396 
  84.  
  85. [mysqldump] 
  86. quick 
  87. max_allowed_packet = 32M 
  1. #添加mysql到服务 
  2. vi /etc/rc.d/init.d/mysqld 
  3.  
  4. #!/bin/sh 
  5. basedir=/app/local/mysql 
  6. datadir=/app/data/mysql/data 
  7. service_startup_timeout=900 
  8. lockdir='/var/lock/subsys' 
  9. lock_file_path="$lockdir/mysql" 
  10. mysqld_pid_file_path=/app/data/mysql/mysql.pid 
  11. if test -z "$basedir" 
  12. then 
  13.   basedir=/usr/local/mysql 
  14.   bindir=/usr/local/mysql/bin 
  15.   if test -z "$datadir" 
  16.   then 
  17.     datadir=/usr/local/mysql/data 
  18.   fi 
  19.   sbindir=/usr/local/mysql/bin 
  20.   libexecdir=/usr/local/mysql/bin 
  21. else 
  22.   bindir="$basedir/bin" 
  23.   if test -z "$datadir" 
  24.   then 
  25.     datadir="$basedir/data" 
  26.   fi 
  27.   sbindir="$basedir/sbin" 
  28.   libexecdir="$basedir/libexec" 
  29. fi 
  30. datadir_set= 
  31. lsb_functions="/lib/lsb/init-functions" 
  32. if test -f $lsb_functions ; then 
  33.   . $lsb_functions 
  34. else 
  35.   log_success_msg() 
  36.   { 
  37.     echo " SUCCESS! $@" 
  38.   } 
  39.   log_failure_msg() 
  40.   { 
  41.     echo " ERROR! $@" 
  42.   } 
  43. fi 
  44.   
  45. PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin" 
  46. export PATH 
  47.   
  48. mode=$1    # start or stop 
  49.   
  50. [ $# -ge 1 ] && shift 
  51.   
  52.   
  53. other_args="$*"   # uncommon, but needed when called from an RPM upgrade action 
  54.            # Expected: "--skip-networking --skip-grant-tables" 
  55.            # They are not checked here, intentionally, as it is the resposibility 
  56.            # of the "spec" file author to give correct arguments only
  57.   
  58. case `echo "testing\c"`,`echo -n testing` in 
  59.     *c*,-n*) echo_n=   echo_c=     ;; 
  60.     *c*,*)   echo_n=-n echo_c=     ;; 
  61.     *)       echo_n=   echo_c='\c' ;; 
  62. esac 
  63.   
  64. parse_server_arguments() { 
  65.   for arg do 
  66.     case "$arg" in 
  67.       --basedir=*)  basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` 
  68.                     bindir="$basedir/bin" 
  69.             if test -z "$datadir_set"then 
  70.               datadir="$basedir/data" 
  71.             fi 
  72.             sbindir="$basedir/sbin" 
  73.             libexecdir="$basedir/libexec" 
  74.         ;; 
  75.       --datadir=*)  datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` 
  76.             datadir_set=1 
  77.     ;; 
  78.       --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; 
  79.       --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; 
  80.     esac 
  81.   done 
  82.   
  83. wait_for_pid () { 
  84.   verb="$1"           # created | removed 
  85.   pid="$2"            # process ID of the program operating on the pid-file 
  86.   pid_file_path="$3" # path to the PID file. 
  87.   
  88.   i=0 
  89.   avoid_race_condition="by checking again" 
  90.   
  91.   while test $i -ne $service_startup_timeout ; do 
  92.   
  93.     case "$verb" in 
  94.       'created'
  95.         # wait for a PID-file to pop into existence. 
  96.         test -s "$pid_file_path" && i='' && break 
  97.         ;; 
  98.       'removed'
  99.         # wait for this PID-file to disappear 
  100.         test ! -s "$pid_file_path" && i='' && break 
  101.         ;; 
  102.       *) 
  103.         echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path" 
  104.         exit 1 
  105.         ;; 
  106.     esac 
  107.   
  108.     # if server isn't running, then pid-file will never be updated 
  109.     if test -n "$pid"then 
  110.       if kill -0 "$pid" 2>/dev/nullthen 
  111.         :  # the server still runs 
  112.       else 
  113.         # The server may have exited between the last pid-file check and now.   
  114.         if test -n "$avoid_race_condition"then 
  115.           avoid_race_condition="" 
  116.           continue  # Check again. 
  117.         fi 
  118.   
  119.         # there's nothing that will affect the file. 
  120.         log_failure_msg "The server quit without updating PID file ($pid_file_path)." 
  121.         return 1  # not waiting any more. 
  122.       fi 
  123.     fi 
  124.   
  125.     echo $echo_n ".$echo_c" 
  126.     i=`expr $i + 1` 
  127.     sleep 1 
  128.   
  129.   done 
  130.   
  131.   if test -z "$i" ; then 
  132.     log_success_msg 
  133.     return 0 
  134.   else 
  135.     log_failure_msg 
  136.     return 1 
  137.   fi 
  138.   
  139. # Get arguments from the my.cnf file, 
  140. # the only group, which is read from now on is [mysqld] 
  141. if test -x ./bin/my_print_defaults 
  142. then 
  143.   print_defaults="./bin/my_print_defaults" 
  144. elif test -x $bindir/my_print_defaults 
  145. then 
  146.   print_defaults="$bindir/my_print_defaults" 
  147. elif test -x $bindir/mysql_print_defaults 
  148. then 
  149.   print_defaults="$bindir/mysql_print_defaults" 
  150. else 
  151.   # Try to find basedir in /etc/my.cnf 
  152.   conf=/etc/my.cnf 
  153.   print_defaults= 
  154.   if test -r $conf 
  155.   then 
  156.     subpat='^[^=]*basedir[^=]*=\(.*\)$' 
  157.     dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf` 
  158.     for d in $dirs 
  159.     do 
  160.       d=`echo $d | sed -e 's/[     ]//g'
  161.       if test -x "$d/bin/my_print_defaults" 
  162.       then 
  163.         print_defaults="$d/bin/my_print_defaults" 
  164.         break 
  165.       fi 
  166.       if test -x "$d/bin/mysql_print_defaults" 
  167.       then 
  168.         print_defaults="$d/bin/mysql_print_defaults" 
  169.         break 
  170.       fi 
  171.     done 
  172.   fi 
  173.   
  174.   # Hope it's in the PATH ... but I doubt it 
  175.   test -z "$print_defaults" && print_defaults="my_print_defaults" 
  176. fi 
  177.   
  178. Read defaults file from 'basedir'.   If there is no defaults file there 
  179. check if it's in the old (depricated) place (datadir) and read it from there 
  180.   
  181. extra_args="" 
  182. if test -r "$basedir/my.cnf" 
  183. then 
  184.   extra_args="-e $basedir/my.cnf" 
  185. else 
  186.   if test -r "$datadir/my.cnf" 
  187.   then 
  188.     extra_args="-e $datadir/my.cnf" 
  189.   fi 
  190. fi 
  191.   
  192. parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server` 
  193.   
  194. Set pid file if not given 
  195. if test -z "$mysqld_pid_file_path" 
  196. then 
  197.   mysqld_pid_file_path=$datadir/`hostname`.pid 
  198. else 
  199.   case "$mysqld_pid_file_path" in 
  200.     /* ) ;; 
  201.     * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;; 
  202.   esac 
  203. fi 
  204.   
  205. case "$mode" in 
  206.   'start'
  207.     # Start daemon 
  208.   
  209.     # Safeguard (relative paths, core dumps..) 
  210.     cd $basedir 
  211.   
  212.     echo $echo_n "Starting MySQL" 
  213.     if test -x $bindir/mysqld_safe 
  214.     then 
  215.       # Give extra arguments to mysqld with the my.cnf file. This script 
  216.       # may be overwritten at next upgrade. 
  217.       $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 & 
  218.       wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? 
  219.   
  220.       # Make lock for RedHat / SuSE 
  221.       if test -w "$lockdir" 
  222.       then 
  223.         touch "$lock_file_path" 
  224.       fi 
  225.   
  226.       exit $return_value 
  227.     else 
  228.       log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" 
  229.     fi 
  230.     ;; 
  231.   
  232.   'stop'
  233.     # Stop daemon. We use a signal here to avoid having to know the 
  234.     # root password
  235.   
  236.     if test -s "$mysqld_pid_file_path" 
  237.     then 
  238.       mysqld_pid=`cat "$mysqld_pid_file_path"
  239.   
  240.       if (kill -0 $mysqld_pid 2>/dev/null
  241.       then 
  242.         echo $echo_n "Shutting down MySQL" 
  243.         kill $mysqld_pid 
  244.         # mysqld should remove the pid file when it exits, so wait for it. 
  245.         wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$? 
  246.       else 
  247.         log_failure_msg "MySQL server process #$mysqld_pid is not running!" 
  248.         rm "$mysqld_pid_file_path" 
  249.       fi 
  250.   
  251.       # Delete lock for RedHat / SuSE 
  252.       if test -f "$lock_file_path" 
  253.       then 
  254.         rm -f "$lock_file_path" 
  255.       fi 
  256.       exit $return_value 
  257.     else 
  258.       log_failure_msg "MySQL server PID file could not be found!" 
  259.     fi 
  260.     ;; 
  261.   
  262.   'restart'
  263.     # Stop the service and regardless of whether it was 
  264.     # running or not, start it again. 
  265.     if $0 stop  $other_args; then 
  266.       $0 start $other_args 
  267.     else 
  268.       log_failure_msg "Failed to stop running server, so refusing to try to start." 
  269.       exit 1 
  270.     fi 
  271.     ;; 
  272.   
  273.   'reload'|'force-reload'
  274.     if test -s "$mysqld_pid_file_path" ; then 
  275.       read mysqld_pid <  "$mysqld_pid_file_path" 
  276.       kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL" 
  277.       touch "$mysqld_pid_file_path" 
  278.     else 
  279.       log_failure_msg "MySQL PID file could not be found!" 
  280.       exit 1 
  281.     fi 
  282.     ;; 
  283.   'status'
  284.     # Firstcheck to see if pid file exists 
  285.     if test -s "$mysqld_pid_file_path" ; then  
  286.       read mysqld_pid < "$mysqld_pid_file_path" 
  287.       if kill -0 $mysqld_pid 2>/dev/null ; then  
  288.         log_success_msg "MySQL running ($mysqld_pid)" 
  289.         exit 0 
  290.       else 
  291.         log_failure_msg "MySQL is not running, but PID file exists" 
  292.         exit 1 
  293.       fi 
  294.     else 
  295.       # Try to find appropriate mysqld process 
  296.       mysqld_pid=`pidof $libexecdir/mysqld` 
  297.   
  298.       # test if multiple pids exist 
  299.       pid_count=`echo $mysqld_pid | wc -w` 
  300.       if test $pid_count -gt 1 ; then 
  301.         log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)" 
  302.         exit 5 
  303.       elif test -z $mysqld_pid ; then  
  304.         if test -f "$lock_file_path" ; then  
  305.           log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists" 
  306.           exit 2 
  307.         fi  
  308.         log_failure_msg "MySQL is not running" 
  309.         exit 3 
  310.       else 
  311.         log_failure_msg "MySQL is running but PID file could not be found" 
  312.         exit 4 
  313.       fi 
  314.     fi 
  315.     ;; 
  316.     *) 
  317.       # usage 
  318.       basename=`basename "$0"
  319.       echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]" 
  320.       exit 1 
  321.     ;; 
  322. esac 
  323.   
  324. exit 0 
  1. #修改权限 
  2. chmod +x /etc/init.d/mysqld 
  3. service mysqld start 
  4.  
  5. #增加MySQL系统环境变量 
  6. echo 'export PATH=$PATH:/app/local/mysql/bin'>>/etc/profile && source /etc/profile 
  7.  
  8. #查看错误日志 
  9. tail -f /var/log/mysqld.log  
  10.  
  11. #用root账户登录并作简单的安全设置 
  12. /app/local/mysql/bin/mysql -uroot -p 
  1. #修改root密码 
  2. UPDATE mysql.user SET Password=password('root'WHERE User='root'
  3.  
  4. #删除无名用户 
  5. DELETE FROM mysql.user WHERE User=''
  6.  
  7. #删除root远程访问 
  8. DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost''127.0.0.1''::1'); 
  9.  
  10. #删除“test”数据库 
  11. DROP database test; 
  12.  
  13. #允许远程访问 
  14. /app/local/mysql/bin/mysql -uroot -proot 
  15.  
  16. use mysql; 
  17. update user set host='%' where user='root' AND host='localhost'
  18. FLUSH PRIVILEGES
  19. select host, user from user
  20.  
  21. #立即生效并退出MYSQL命令窗体 
  22. FLUSH PRIVILEGES;QUIT; 

  #p#

六、安装Apache

  1. cd /app/local 
  2. tar zxvf httpd-2.2.29.tar.gz 
  3. cd httpd-2.2.29 
  4.  
  5. ./configure --prefix=/app/local/apache \ 
  6. --enable-so \ 
  7. --enable-rewrite \ 
  8. --enable-modes-shared=most 
  9.  
  10. make && make install  
  11.  
  12. vi /app/local/apache/conf/httpd.conf 
  13.  
  14. #修改主机名 
  15. ServerName localhost:80 
  16. #查找AddType application/x-gzip .gz .tgz,在该行下面添加 
  17. AddType application/x-httpd-php .php 
  18. #查找DirectoryIndex index.html 把该行修改成 
  19. DirectoryIndex index.html index.htm index.php 
  20.  
  21. /app/local/apache/bin/apachectl -t 
  22. cp /app/local/apache/bin/apachectl /etc/init.d/httpd 

  七、安装PHP

  1.PHP基础环境

  1. #yum安装或者使用下面源包编译安装 
  2. yum install libmcrypt libmcrypt-devel mcrypt mhash 
  3.  
  4. #下载地址 
  5. http://sourceforge.net/projects/mcrypt/files/Libmcrypt/ 
  6. http://sourceforge.net/projects/mcrypt/files/MCrypt/ 
  7. http://sourceforge.net/projects/mhash/files/mhash/ 
  8.  
  9. #安装Libmcrypt 
  10. tar -zxvf libmcrypt-2.5.8.tar.gz 
  11. cd libmcrypt-2.5.8 
  12. ./configure 
  13. make && make install 
  14. cd ../ 
  15.  
  16. 3.安装mhash 
  17.  
  18. tar -zxvf mhash-0.9.9.9.tar.gz 
  19. cd mhash-0.9.9.9 
  20. ./configure 
  21. make && make install 
  22. cd ../ 
  23.  
  24. 4.安装mcrypt 
  25.  
  26. tar -zxvf mcrypt-2.6.8.tar.gz 
  27. cd mcrypt-2.6.8 
  28. LD_LIBRARY_PATH=/usr/local/lib ./configure 
  29. make && make install 
  30. cd ../ 
  31.  
  32. ### 安装PHP 
  33.  
  34. >extension根据需要定制,新增的OPcache建议暂时不要开启 
  35.  
  36. ``` bash 
  37. tar zxvf php-5.5.27.tar.gz 
  38. cd php-5.5.27 
  39.  
  40. ./configure --prefix=/app/local/php \ 
  41. --with-config-file-path=/app/local/php/etc \ 
  42. --enable-fpm \ 
  43. --enable-mbstring \ 
  44. --with-mhash \ 
  45. --with-mcrypt \ 
  46. --with-curl \ 
  47. --with-openssl \ 
  48. --with-mysql=mysqlnd \ 
  49. --with-mysqli=mysqlnd \ 
  50. --with-pdo-mysql=mysqlnd \ 
  51. --with-apxs2=/app/local/apache/bin/apxs  
  52. #--enable-opcache 
  53.  
  54. make && make install 
  55.  
  56. #配置php.ini 
  57. cp php.ini-development /app/local/php/etc/php.ini 
  58.  
  59. #设置时区 
  60. sed -i "s#;date.timezone =#date.timezone = Asia/Shanghai#g" /app/local/php/etc/php.ini 
  61. #防止nginx文件类型错误解析漏洞 
  62. sed -i "s#;cgi.fix_pathinfo=1#cgi.fix_pathinfo=0#g" /app/local/php/etc/php.ini 
  63. #禁止显示php版本的信息 
  64. sed -i "s#expose_php = On#expose_php = Off#g" /app/local/php/etc/php.ini 
  65. #禁用危险函数(可选) 
  66. #sed -i "s#disable_functions =#disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source#g" /app/local/php/etc/php.ini 
  67.  
  68. #enable-opcache后设置(可选) 
  69. [OPcache] 
  70. zend_extension = opcache.so 
  71. opcache.enable=1 
  72. opcache.memory_consumption = 64 
  73. opcache.interned_strings_buffer = 8 
  74. opcache.max_accelerated_files = 4000 
  75. opcache.revalidate_freq = 60 
  76. opcache.fast_shutdown = 1 
  77. opcache.enable_cli = 1 

  #p#

2.配置php-fpm

  1. #编辑php-fpm 
  2. cp /app/local/php/etc/php-fpm.conf.default /app/local/php/etc/php-fpm.conf 
  3. vi /app/local/php/etc/php-fpm.conf 
  4.  
  5. [global
  6. ;错误日志 
  7. error_log = log/php-fpm.log 
  8. ;错误日志级别 
  9. log_level = notice 
  10. [www] 
  11. ;php-fpm监听端口 
  12. listen = 127.0.0.1:9000 
  13. ;启动进程的帐户和组 
  14. user = www 
  15. group = www 
  16. ;如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由后面3个参数动态决定 
  17. pm = dynamic 
  18. ;子进程最大数 
  19. pm.max_children = 384 
  20. ;启动时的进程数 
  21. pm.start_servers = 20 
  22. ;保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程 
  23. pm.min_spare_servers = 5 
  24. ;保证空闲进程数最大值,如果空闲进程大于此值,此进行清理 
  25. pm.max_spare_servers = 35 
  26.  
  27. ;设置每个子进程重生之前服务的请求数。对于可能存在内存泄漏的第三方模块来说是非常有用的。如果设置为 '0' 则一直接受请求。等同于 PHP_FCGI_MAX_REQUESTS 环境变量。默认值: 0。 
  28. pm.max_requests = 1000 
  29. ;每个子进程闲置多长时间就自杀 
  30. pm.process_idle_timeout = 10s 
  31. ;设置单个请求的超时中止时间。该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用。设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。 
  32. request_terminate_timeout = 120 
  33. ;当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中。设置为 '0' 表示 'Off' 
  34. request_slowlog_timeout = 3s 
  35. ;慢请求的记录日志,配合request_slowlog_timeout使用 
  36. slowlog = /app/local/php/var/log/php-fpm.slow.log 
  37. ;设置文件打开描述符的rlimit限制。默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。 
  38. rlimit_files = 65535 
  1. #设置php环境变量 
  2. echo 'export PATH=$PATH:/app/local/php/bin'>>/etc/profile && source /etc/profile 
  3. touch /app/local/php/var/log/php-fpm.slow.log 
  4.  
  5. #添加php-fpm服务 
  6. cp /app/local/php-5.5.27/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 
  7. chmod +x /etc/rc.d/init.d/php-fpm 
  8. service php-fpm start 
  9.  
  10. #设置开机自动启动服务 
  11. vi /etc/rc.local 
  12.  
  13. ulimit -SHn 65535 
  14. service php-fpm start 
  15. service nginx start 
  16. service mysqld start 

  3.配置memcache/mongo/redis

  其它extension扩展都可以动态添加,没事的

  1. #memcache 
  2. cd /app/local 
  3. tar zxvf memcache-3.0.8.tgz 
  4. cd memcache-3.0.8 
  5. /app/local/php/bin/phpize 
  6. ./configure --enable-memcache \ 
  7. --with-php-config=/app/local/php/bin/php-config \ 
  8. --with-zlib-dir 
  9. make && make install 
  10.  
  11. #mongo 
  12. cd /app/local 
  13. tar zxvf mongo-1.6.10.tgz 
  14. cd mongo-1.6.10 
  15. /app/local/php/bin/phpize 
  16. ./configure --with-php-config=/app/local/php/bin/php-config 
  17. make && make install 
  18.  
  19. #redis 
  20. cd /app/local 
  21. tar zxvf redis-2.2.7.tgz 
  22. cd redis-2.2.7 
  23. /app/local/php/bin/phpize 
  24. ./configure --with-php-config=/app/local/php/bin/php-config 
  25. make && make install 
  26.  
  27. #php.ini 
  28. vi /app/local/php/etc/php.ini   
  29.  
  30. [memcached]   
  31. extension=memcached.so 
  32. [mongodb]   
  33. extension=mongo.so  
  34. [redis]   
  35. extension=redis.so  
  36.  
  37. #重启生效 
  38. service php-fpm restart 
  39. php -i | grep php.ini 
  40. php -m 

  八、自动化部署

  服务器的上传目录可以自定义,安装目录默认统一修改为/app/{local,data},执行脚本为web.sh。

  1. file://E:\QQDownload\LTMP     (2 folders, 5 files, 27.66 MB, 30.76 MB in total.) 
  2. │  httpd-2.2.29.tar.gz     7.19 MB 
  3. │  pcre-8.37.tar.gz     1.95 MB 
  4. │  php-5.5.27.tar.gz     16.95 MB 
  5. │  tengine-2.1.0.tar.gz     1.58 MB 
  6. │  web.sh     4.10 KB 
  7. ├─init     (1 folders, 12 files, 91.42 KB, 92.23 KB in total.) 
  8. │  │  allow.conf     35 bytes 
  9. │  │  bashrc     2.99 KB 
  10. │  │  deny.conf     35 bytes 
  11. │  │  limits.conf     1.86 KB 
  12. │  │  my.cnf     1.99 KB 
  13. │  │  mysqld     8.39 KB 
  14. │  │  nginx     2.22 KB 
  15. │  │  nginx.conf     1.34 KB 
  16. │  │  php-fpm     2.30 KB 
  17. │  │  php-fpm.conf     416 bytes 
  18. │  │  php.ini     67.83 KB 
  19. │  │  sysctl.conf     2.03 KB 
  20. │  └─vhosts     (0 folders, 1 files, 826 bytes, 826 bytes in total.) 
  21. │          localhost.conf     826 bytes 
  22. └─src     (0 folders, 6 files, 3.01 MB, 3.01 MB in total.) 
  23.         libmcrypt-2.5.8.tar.gz     1.27 MB 
  24.         mcrypt-2.6.8.tar.gz     460.85 KB 
  25.         memcache-3.0.8.tgz     68.87 KB 
  26.         mhash-0.9.9.9.tar.gz     909.61 KB 
  27.         mongo-1.6.10.tgz     204.19 KB 
  28.         redis-2.2.7.tgz     131.19 KB 
  29.  
  30.  
  31. #web.sh 
  32.  
  33. #!/bin/bash 
  34.  
  35. ## alias 
  36. ltmp_local=$(cd "$(dirname "$0")"; pwd) 
  37. mkdir -p /app/{local,data} 
  38. unalias cp 
  39. ltmp_init=$ltmp_local/init/ 
  40. ltmp_src=$ltmp_local/src/ 
  41.  
  42. ## system 
  43.  
  44. #history 
  45. cp ${ltmp_init}bashrc /etc/ 
  46. #time 
  47. rm -rf /etc/localtime 
  48. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
  49. #maildrop 
  50. sed 's/MAILTO=root/MAILTO=""/g' /etc/crontab 
  51. service crond restart 
  52. #selinux 
  53. setenforce 0 
  54. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
  55. #limits 
  56. echo ulimit -SHn 65535 >> /etc/profile 
  57. source /etc/profile 
  58. cp ${ltmp_init}limits.conf /etc/security/ 
  59. #tcp 
  60. cp ${ltmp_init}sysctl.conf  /etc/ 
  61. #yum 
  62. yum -y install yum-fastestmirror 
  63. yum remove httpd mysql mysql-server php php-cli php-common php-devel php-gd  -y 
  64. yum install -y wget gcc gcc-c++ openssl* curl curl-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel gd gd2 gd-devel gd2-devel libaio autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel 
  65. #download 
  66.  
  67. cd /app/local 
  68. ##PCRE - Perl Compatible Regular Expressions 
  69. #wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz" 
  70. ##Tengine 
  71. #wget "http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" 
  72. ##MySQL 
  73. #wget "https://downloads.mariadb.com/archives/mysql-5.6/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz" 
  74. ##PHP 
  75. #wget "http://cn2.php.net/distributions/php-5.6.11.tar.gz" 
  76. ##Mhash 
  77. #wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz" 
  78. ##libmcrypt 
  79. #wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz" 
  80. ##Mcrypt 
  81. #wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz" 
  82.  
  83. ## soft 
  84. cd $ltmp_local 
  85. #pcre 
  86. tar zxvf pcre-8.37.tar.gz 1> /dev/null 
  87. cd pcre-8.37 
  88. ./configure 
  89. make && make install 
  90. cd ../ 
  91. #tengine 
  92. groupadd www 
  93. useradd -g www www 
  94. #安装Tengine 
  95. cd $ltmp_local 
  96. tar zxvf tengine-2.1.0.tar.gz 1> /dev/null 
  97. cd tengine-2.1.0 
  98. ./configure --user=www --group=www \ 
  99. --prefix=/app/local/nginx \ 
  100. --with-http_stub_status_module \ 
  101. --with-http_ssl_module \ 
  102. --with-pcre=${ltmp_local}/pcre-8.37 
  103. make && make install 
  104. cd ../ 
  105. #nginx config 
  106. cd $ltmp_local 
  107. cp ${ltmp_init}nginx.conf /app/local/nginx/conf/ 
  108. cp -r ${ltmp_init}vhosts /app/local/nginx/conf/ 
  109. mkdir -p /app/data/localhost 
  110. chmod +w /app/data/localhost 
  111. echo "<?php phpinfo();?>" > /app/data/localhost/phpinfo.php 
  112. chown -R www:www /app/data/localhost 
  113. echo 'export PATH=$PATH:/app/local/nginx/sbin'>>/etc/profile && source /etc/profile 
  114. cp ${ltmp_init}nginx /etc/rc.d/init.d/ 
  115. chmod +x /etc/init.d/nginx 
  116. ulimit -SHn 65535 
  117. service nginx start 
  118. #libmcrypt 
  119. cd $ltmp_src 
  120. tar -zxvf libmcrypt-2.5.8.tar.gz 1> /dev/null 
  121. cd libmcrypt-2.5.8 
  122. ./configure 
  123. make && make install 
  124. cd ../ 
  125. #mhash 
  126. cd $ltmp_src 
  127. tar -zxvf mhash-0.9.9.9.tar.gz 1> /dev/null 
  128. cd mhash-0.9.9.9 
  129. ./configure 
  130. make && make install 
  131. cd ../ 
  132. #mcrypt 
  133. cd $ltmp_src 
  134. tar -zxvf mcrypt-2.6.8.tar.gz 1> /dev/null 
  135. cd mcrypt-2.6.8 
  136. LD_LIBRARY_PATH=/usr/local/lib ./configure 
  137. make && make install 
  138. cd ../ 
  139. #php 
  140. cd $ltmp_local 
  141. tar zxvf php-5.5.27.tar.gz 1> /dev/null 
  142. cd php-5.5.27 
  143. ./configure --prefix=/app/local/php \ 
  144. --with-config-file-path=/app/local/php/etc \ 
  145. --enable-fpm \ 
  146. --enable-mbstring \ 
  147. --with-mhash \ 
  148. --with-mcrypt \ 
  149. --with-curl \ 
  150. --with-openssl \ 
  151. --with-mysql=mysqlnd \ 
  152. --with-mysqli=mysqlnd \ 
  153. --with-pdo-mysql=mysqlnd 
  154. make && make install 
  155. #memcache 
  156. cd $ltmp_src 
  157. tar zxvf memcache-3.0.8.tgz  1> /dev/null 
  158. cd memcache-3.0.8 
  159. /app/local/php/bin/phpize 
  160. ./configure --enable-memcache \ 
  161. --with-php-config=/app/local/php/bin/php-config \ 
  162. --with-zlib-dir 
  163. make && make install 
  164. #mongo 
  165. cd $ltmp_src 
  166. tar zxvf mongo-1.6.10.tgz  1> /dev/null 
  167. cd mongo-1.6.10 
  168. /app/local/php/bin/phpize 
  169. ./configure --with-php-config=/app/local/php/bin/php-config 
  170. make && make install 
  171. #redis 
  172. cd $ltmp_src 
  173. #redis 
  174. tar zxvf redis-2.2.7.tgz  1> /dev/null 
  175. cd redis-2.2.7 
  176. /app/local/php/bin/phpize 
  177. ./configure --with-php-config=/app/local/php/bin/php-config 
  178. make && make install 
  179. #php-fpm 
  180. cp ${ltmp_init}php.ini /app/local/php/etc/ 
  181. cp ${ltmp_init}php-fpm.conf /app/local/php/etc/ 
  182. echo 'export PATH=$PATH:/app/local/php/bin'>>/etc/profile && source /etc/profile 
  183. touch /app/local/php/var/log/php-fpm.slow.log 
  184. cp ${ltmp_local}/php-5.5.27/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 
  185. chmod +x /etc/rc.d/init.d/php-fpm 
  186. service php-fpm start 
责任编辑:火凤凰 来源: HelloDog博客
相关推荐

2017-09-21 16:06:43

DevOps自动化测试代码

2013-11-27 11:34:43

自动化部署Python

2021-10-13 10:06:49

自动化IT安全

2022-02-04 21:50:37

网络安全自动化

2012-11-20 17:22:57

2015-05-25 19:34:06

KickstartCentOS

2018-06-07 11:00:27

NFVSDN部署

2017-10-01 06:24:13

2017-07-24 09:24:50

2021-06-28 22:43:20

安全自动化安全运营中心SOC

2023-08-02 15:33:27

2021-03-03 14:50:55

安全自动化机器学习网络安全

2021-09-03 09:56:18

鸿蒙HarmonyOS应用

2022-02-10 12:04:45

网络安全自动化

2021-09-07 09:00:00

开发测试工具

2024-01-23 10:58:49

2023-03-29 08:33:03

仓储自动化系统

2013-04-17 15:48:51

2023-05-24 16:51:13

2009-08-12 18:37:46

VSTS2005单元测
点赞
收藏

51CTO技术栈公众号