免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2887 | 回复: 1
打印 上一主题 下一主题

Ubuntu Linux 10.04 安装及配置Nginx+PHP FPM [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-02 14:25 |只看该作者 |倒序浏览
Ubuntu Linux 10.04 安装及配置Nginx+PHP FPM










Bash代码
  1. 1.#!/bin/bash   
  2. 2.##########################################   
  3. 3.#        Install app server env.   
  4. 4.#   Prepare:Ubuntu 10.04 Linux server configed ssh,LVS Real Server and mysql slave.   
  5. 5.##########################################   
  6. 6.[ `whoami` != "root" ] && echo "Not root." && exit 1;   
  7. 7.export EDITOR=vim;   
  8. 8.if ! grep "export EDITOR=vim" /etc/profile >/dev/null;   
  9. 9.then   
  10. 10.    echo "export EDITOR=vim;" >> /etc/profile;   
  11. 11.fi;   
  12. 12.  
  13. 13.#app server domain   
  14. 14.DOMAIN='app.example.net';   
  15. 15.#statics files server domain   
  16. 16.S_DOMAIN='statics.app.example.net';   
  17. 17.  
  18. 18.#Linux内核参数优化   
  19. 19.sysctl -w net.ipv4.tcp_syncookies=1 #表示开启SYN   
  20. 20.Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭   
  21. 21.sysctl -w net.ipv4.tcp_tw_reuse=1 #表示开启重用。允许将TIME-WAIT   
  22. 22.sockets重新用于新的TCP连接,默认为0,表示关闭   
  23. 23.sysctl -w net.ipv4.tcp_tw_recycle=1 # 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭   
  24. 24.sysctl -w net.ipv4.tcp_fin_timeout=30 #表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间   
  25. 25.sysctl -w net.ipv4.tcp_max_tw_buckets=6000  #系统同时保持TIME_WAIT套接字的最大数量   
  26. 26.sysctl -w net.core.somaxconn=262144  
  27. 27.#表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。   
  28. 28.sysctl -w net.ipv4.tcp_keepalive_time = 1200  
  29. 29.#表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。   
  30. 30.sysctl -w net.ipv4.ip_local_port_range = 1024 65000  
  31. 31.#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。   
  32. 32.sysctl -w net.ipv4.tcp_max_syn_backlog = 8192  
  33. 33.#表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。   
  34. 34.  
  35. 35.sysctl > /etc/sysctl.conf;   
  36. 36.  
  37. 37.( #Start   
  38. 38.  
  39. 39.  
  40. 40.#Install  production server   
  41. 41.(   
  42. 42.apt-get -y --force-yes install curl;#安装curl   
  43. 43.apt-get -y --force-yes install python-software-properties;   
  44. 44.add-apt-repository ppa:brianmercer/php;#Ubuntu 10.04 需要添加PHP FPM的PPA源   
  45. 45.apt-get update;   
  46. 46.  
  47. 47.apt-get -y --force-yes install nginx;   
  48. 48.apt-get -y --force-yes install memcached;   
  49. 49.apt-get -y --force-yes install mercurial;   
  50. 50.apt-get -y --force-yes install php5-cgi php5-fpm php-apc php5-mysql php5-gd php5-mcrypt php5-memcache;   
  51. 51.) > /dev/null;   
  52. 52.  
  53. 53.#fix "#" comment   
  54. 54.echo 'extension=mcrypt.so' > /etc/php5/fpm/conf.d/mcrypt.ini;   
  55. 55.  
  56. 56.  
  57. 57.#Deploy app   
  58. 58.  
  59. 59.cd /var/www;   
  60. 60.rm -rf app;   
  61. 61.hg clone https://repo.app@repo.dev.example.net/hg/app/;   
  62. 62.  
  63. 63.  
  64. 64.  
  65. 65.#Config nginx   
  66. 66.#我们服务器有16核,所以...   
  67. 67.echo '   
  68. 68.user www-data;   
  69. 69.worker_processes  16;   
  70. 70.worker_cpu_affinity 1000000000000000 0100000000000000 0010000000000000 0001000000000000 0000100000000000 0000010000000000 0000001000000000 0000000100000000 0000000010000000 0000000001000000 0000000000100000 0000000000010000 0000000000001000 0000000000000100 0000000000000010 0000000000000001;   
  71. 71.worker_rlimit_nofile 65536;   
  72. 72.  
  73. 73.error_log /var/log/nginx/error.log;   
  74. 74.pid  /var/run/nginx.pid;   
  75. 75.events {   
  76. 76.    use epoll;   
  77. 77.    worker_connections 131072;   
  78. 78.}   
  79. 79.  
  80. 80.http {   
  81. 81.    client_header_buffer_size   4K;   
  82. 82.    open_file_cache max=65536 inactive=20s;   
  83. 83.    open_file_cache_min_uses 3;   
  84. 84.    open_file_cache_valid 30s;   
  85. 85.      
  86. 86.    access_log  off;   
  87. 87.    include /etc/nginx/mime.types;   
  88. 88.      
  89. 89.    sendfile    on;   
  90. 90.      
  91. 91.    tcp_nopush  on;   
  92. 92.    tcp_nodelay on;   
  93. 93.  
  94. 94.    gzip  on;   
  95. 95.    gzip_disable "MSIE [1-6]\.(?!.*SV1)";   
  96. 96.    gzip_buffers 16 64k;   
  97. 97.    gzip_min_length 1k;   
  98. 98.    gzip_comp_level 6;   
  99. 99.    gzip_vary on;   
  100. 100.    gzip_types text/plain text/javascript text/css application/x-javascript text/xml application/xml application/xml+rss;   
  101. 101.      
  102. 102.    include /etc/nginx/conf.d/*.conf;   
  103. 103.    include /etc/nginx/sites-enabled/*;   
  104. 104.}   
  105. 105.' > /etc/nginx/nginx.conf;   
  106. 106.  
  107. 107.#enable nginx-status   
  108. 108.echo "   
  109. 109.server {   
  110. 110.    listen   80 default;   
  111. 111.    server_name  localhost;   
  112. 112.  
  113. 113.    access_log off;   
  114. 114.  
  115. 115.    location / {   
  116. 116.        root   /var/www/nginx-default;   
  117. 117.        index  index.html index.htm;   
  118. 118.    }   
  119. 119.    location = /favicon.ico {   
  120. 120.        log_not_found off;   
  121. 121.    }   
  122. 122.  
  123. 123.    location /nginx-status {   
  124. 124.        stub_status on;   
  125. 125.        allow 127.0.0.1;   
  126. 126.        deny all;   
  127. 127.    }   
  128. 128.}" > /etc/nginx/sites-enabled/default;   
  129. 129.  
  130. 130.echo '   
  131. 131.server {   
  132. 132.    listen 80;   
  133. 133.    server_name '$DOMAIN';   
  134. 134.      
  135. 135.    keepalive_timeout 0;   
  136. 136.      
  137. 137.    access_log  off;   
  138. 138.    log_not_found off;   
  139. 139.    error_log /var/log/nginx/app.error.log;   
  140. 140.      
  141. 141.    root /var/www/app/;   
  142. 142.    index index.php index.htm index.html;   
  143. 143.      
  144. 144.    location / {   
  145. 145.        try_files $uri $uri/ /index.php?$args;   
  146. 146.    }   
  147. 147.      
  148. 148.    location ~ ^/(protected|yii)/ {   
  149. 149.        deny all;   
  150. 150.    }   
  151. 151.      
  152. 152.    location = /favicon.ico {   
  153. 153.        expires max;   
  154. 154.        return 204;   
  155. 155.    }   
  156. 156.      
  157. 157.    location ~ \.php$ {   
  158. 158.        fastcgi_pass unix:/dev/shm/app-php-fpm.socket;   
  159. 159.        fastcgi_param PATH_INFO $fastcgi_path_info;   
  160. 160.        fastcgi_index index.php;   
  161. 161.           
  162. 162.        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   
  163. 163.        fastcgi_param SERVER_ADDR $server_addr;   
  164. 164.        fastcgi_param SERVER_PORT $server_port;   
  165. 165.        fastcgi_param REMOTE_ADDR $remote_addr;   
  166. 166.        fastcgi_param REMOTE_PORT $remote_port;   
  167. 167.        fastcgi_param X-Real-IP $remote_addr;   
  168. 168.           
  169. 169.        include fastcgi_params;   
  170. 170.    }   
  171. 171.  
  172. 172.}   
  173. 173.' > /etc/nginx/sites-available/app;   
  174. 174.  
  175. 175.  
  176. 176.echo '   
  177. 177.server {   
  178. 178.    listen 80;   
  179. 179.    server_name '$S_DOMAIN';   
  180. 180.      
  181. 181.    keepalive_timeout 60;   
  182. 182.    access_log  off;   
  183. 183.    log_not_found off;   
  184. 184.      
  185. 185.    index index.htm index.html;   
  186. 186.      
  187. 187.    location / {   
  188. 188.        root /var/www/app/;   
  189. 189.        deny all;   
  190. 190.    }   
  191. 191.      
  192. 192.    location ~ ^/(statics|css|assets|demo|themes|tests)/ {   
  193. 193.        root /var/www/app/;   
  194. 194.        gzip  on;   
  195. 195.        gzip_disable "MSIE [1-6]\.(?!.*SV1)";   
  196. 196.        gzip_buffers 16 64k;   
  197. 197.        gzip_min_length 1k;   
  198. 198.        gzip_comp_level 6;   
  199. 199.        gzip_vary on;   
  200. 200.        expires 7d;   
  201. 201.        add_header Pragma public;   
  202. 202.        add_header Cache-Control "public, must-revalidate, proxy-revalidate";   
  203. 203.    }   
  204. 204.      
  205. 205.    location = /favicon.ico {   
  206. 206.        return 204;   
  207. 207.    }   
  208. 208.      
  209. 209.    location ~ \.php$ {   
  210. 210.        deny all;   
  211. 211.    }   
  212. 212.      
  213. 213.  
  214. 214.}   
  215. 215.' > /etc/nginx/sites-available/s.app;   
  216. 216.  
  217. 217.  
  218. 218.ln -sf ../sites-available/app /etc/nginx/sites-enabled/;   
  219. 219.ln -sf ../sites-available/s.app /etc/nginx/sites-enabled/;   
  220. 220.  
  221. 221.  
  222. 222.  
  223. 223.  
  224. 224.echo '   
  225. 225.[global]   
  226. 226.pid = /var/run/php5-fpm.pid   
  227. 227.error_log = /var/log/php5-fpm-error.log   
  228. 228.process_control_timeout = 30  
  229. 229.daemonize = yes   
  230. 230.[www]   
  231. 231.listen = /dev/shm/app-php-fpm.socket   
  232. 232.user = www-data   
  233. 233.group = www-data   
  234. 234.pm = static   
  235. 235.pm.max_children = 256  
  236. 236.pm.max_requests = 65535  
  237. 237.request_terminate_timeout = 30  
  238. 238.rlimit_files = 65535  
  239. 239.' > /etc/php5/fpm/php5-fpm.conf;   
  240. 240.  
  241. 241.  
  242. 242.  
  243. 243.  
  244. 244.service php5-fpm restart;   
  245. 245.service nginx restart;   
  246. 246.  
  247. 247.  
  248. 248.if ! grep "$DOMAIN" /etc/hosts;then   
  249. 249.    echo "127.0.0.1 $DOMAIN  $S_DOMAIN" >> /etc/hosts;   
  250. 250.fi;   
  251. 251.  
  252. 252.(   
  253. 253.    crontab -l|sed "/$DOMAIN/d";   
  254. 254.    echo "   
  255. 255.      30 * * * * curl http://$DOMAIN/CronTask/some-op   
  256. 256.  
  257. 257.      5 * * * * curl http://$DOMAIN/CronTask/some-op   
  258. 258.    ";   
  259. 259.)|crontab;   
  260. 260.  
  261. 261.  
  262. 262.  
  263. 263.  
  264. 264.  
  265. 265.#End   
  266. 266.);  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-21 22:11 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP