[关闭]
@Great-Chinese 2016-12-05T08:13:21.000000Z 字数 11367 阅读 1123

LNMP架构搭建与优化

LNMP架构搭建与优化


1.1 php编译安装

A:下载与解压

  1. wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2
  2. tar jxvf php-5.4.37.tar.bz2

B:配置编译参数

  1. cd php-5.4.37 # 进入此目录中
  2. ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6

C:编译

  1. make
  2. echo $?

D:安装

  1. rm -rf /usr/local/php/ # 在安装前,要把之前旧的php目录删除掉
  2. make install
  3. echo $?
  1. cp php.ini-production /usr/local/php/etc/php.ini # 拷贝生产环境的配置文件
  2. cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 拷贝启动脚本
  3. vim /etc/init.d/php-fpm # 编辑启动的shell脚本
  4. chmod 755 /etc/init.d/php-fpm # 授予执行权限
  5. cd /usr/local/php/etc/
  6. mv php-fpm.conf.default php-fpm.conf
  7. chkconfig --add php-fpm # 加入系统服务列表中
  8. chkconfig php-fpm on # 开机启动
  9. useradd -s /sbin/nologin php-fpm # 增加用户
  10. service php-fpm start # 启动
  11. /usr/local/php/sbin/php-fpm -t # 检查是否有问题
  12. ps aux|grep php-fpm
  13. netstat -lnp

1.2 Nginx编译安装

A:下载与解压

  1. cd /usr/local/src # 进入此目录中
  2. wget http://nginx.org/download/nginx-1.6.2.tar.gz
  3. tar zxvf nginx-1.6.2.tar.gz

B:配置编译参数

  1. cd nginx-1.6.2 # 进入此目录中
  2. ./configure --prefix=/usr/local/nginx --with-pcre
  3. yum install -y pcre-devel

C:编译/安装

  1. make
  2. make install
  1. cd /usr/local/nginx
  2. /usr/local/apache2/bin/apachectl stop
  3. /usr/local/nginx/sbin/nginx # 启动nginx
  4. ps aux |grep nginx
  5. netstat lnp

1.3 测试php解析

  1. # 打开nginx默认的配置文件
  2. vim /usr/local/nginx/conf/nginx.conf # 打开下面的内容
  3. location ~ \.php$ {
  4. root html;
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_index index.php;
  7. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
  8. include fastcgi_params;
  9. }
  10. cat /usr/local/nginx/html/index.html
  11. cd /usr/local/nginx/html/ #进入html目录
  12. vi info.php # 然后编辑info文件
  13. /usr/local/nginx/sbin/nginx -t # 检查是否有错
  14. /usr/local/nginx/sbin/nginx -s reload # 重启后,在网址上输入IP,就可以解析php了
  15. curl localhost
  16. curl localhost/info.php

1.4 nginx启动脚本和配置文件

A:编写nginx启动脚本,并加入系统服务

  1. vim /etc/init.d/nginx # 加入以下内容
  2. #!/bin/bash
  3. # chkconfig: - 30 21
  4. # description: http service.
  5. # Source Function Library
  6. . /etc/init.d/functions
  7. # Nginx Settings
  8. NGINX_SBIN="/usr/local/nginx/sbin/nginx"
  9. NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
  10. NGINX_PID="/usr/local/nginx/logs/nginx.pid"
  11. RETVAL=0
  12. prog="Nginx"
  13. start() {
  14. echo -n $"Starting $prog: "
  15. mkdir -p /dev/shm/nginx_temp
  16. daemon $NGINX_SBIN -c $NGINX_CONF
  17. RETVAL=$?
  18. echo
  19. return $RETVAL
  20. }
  21. stop() {
  22. echo -n $"Stopping $prog: "
  23. killproc -p $NGINX_PID $NGINX_SBIN -TERM
  24. rm -rf /dev/shm/nginx_temp
  25. RETVAL=$?
  26. echo
  27. return $RETVAL
  28. }
  29. reload(){
  30. echo -n $"Reloading $prog: "
  31. killproc -p $NGINX_PID $NGINX_SBIN -HUP
  32. RETVAL=$?
  33. echo
  34. return $RETVAL
  35. }
  36. restart(){
  37. stop
  38. start
  39. }
  40. configtest(){
  41. $NGINX_SBIN -c $NGINX_CONF -t
  42. return 0
  43. }
  44. case "$1" in
  45. start)
  46. start
  47. ;;
  48. stop)
  49. stop
  50. ;;
  51. reload)
  52. reload
  53. ;;
  54. restart)
  55. restart
  56. ;;
  57. configtest)
  58. configtest
  59. ;;
  60. *)
  61. echo $"Usage: $0 {start|stop|reload|restart|configtest}"
  62. RETVAL=1
  63. esac
  64. exit $RETVAL

B:保存后,更改权限:

  1. chmod 755 /etc/init.d/nginx
  2. chkconfig --add nginx # 加到启动列表中
  3. chkconfig nginx on # 开机启动,执行此条命令

C: nginx的启动,重启,停止,检测

  1. service nginx start
  2. service nginx restart
  3. service nginx stop
  4. service nginx configtest

D:更改nginx配置

  1. vim /usr/local/nginx/conf/nginx.conf #增加以下内容
  2. user nobody nobody;
  3. worker_processes 2;
  4. error_log /usr/local/nginx/logs/nginx_error.log crit;
  5. pid /usr/local/nginx/logs/nginx.pid;
  6. worker_rlimit_nofile 51200;
  7. events
  8. {
  9. use epoll;
  10. worker_connections 6000;
  11. }
  12. http
  13. {
  14. include mime.types;
  15. default_type application/octet-stream;
  16. server_names_hash_bucket_size 3526;
  17. server_names_hash_max_size 4096;
  18. log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  19. '$host "$request_uri" $status'
  20. '"$http_referer" "$http_user_agent"';
  21. sendfile on;
  22. tcp_nopush on;
  23. keepalive_timeout 30;
  24. client_header_timeout 3m;
  25. client_body_timeout 3m;
  26. send_timeout 3m;
  27. connection_pool_size 256;
  28. client_header_buffer_size 1k;
  29. large_client_header_buffers 8 4k;
  30. request_pool_size 4k;
  31. output_buffers 4 32k;
  32. postpone_output 1460;
  33. client_max_body_size 10m;
  34. client_body_buffer_size 256k;
  35. client_body_temp_path /usr/local/nginx/client_body_temp;
  36. proxy_temp_path /usr/local/nginx/proxy_temp;
  37. fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  38. fastcgi_intercept_errors on;
  39. tcp_nodelay on;
  40. gzip on;
  41. gzip_min_length 1k;
  42. gzip_buffers 4 8k;
  43. gzip_comp_level 5;
  44. gzip_http_version 1.1;
  45. gzip_types text/plain application/x-javascript text/css text/htm application/xml;
  46. include vhosts/*.conf;
  47. }
  1. cd /usr/local/nginx/conf
  2. mkdir vhosts
  3. cd vhosts
  4. mkdir /tmp/1233
  5. # 创建虚拟主机配置文件
  6. vim default.conf # 增加内容如下
  7. server
  8. {
  9. listen 80 default_server;
  10. server_name localhost;
  11. index index.html index.htm index.php;
  12. root /tmp/1233;
  13. deny all;
  14. }
  15. /usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错
  16. /etc/init.d/nginx reload # 加载配置文件
  17. curl -x127.0.0.1:80 www.baidu.com
  18. # 创建新的虚拟主机配置文件
  19. vim 111.conf # 增加内容如下
  20. server
  21. {
  22. listen 80;
  23. server_name 111.com;
  24. index index.html index.htm index.php;
  25. root /data/www;
  26. location ~ \.php$ {
  27. include fastcgi_params;
  28. # fastcgi_pass unix:/tmp/php-fcgi.sock;
  29. fastcgi_pass 127.0.0.1:9000;
  30. fastcgi_index index.php;
  31. fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
  32. }
  33. }
  34. /usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错
  35. /etc/init.d/nginx reload # 加载配置文件
  36. curl -x127.0.0.1:80 111.com/forum.php -I

1.5 php-fpm配置文件

  1. /usr/local/php/etc/php-fpm.conf # 管理php-fpm相关服务的配置
  2. /usr/local/php/etc/php.ini # php的全局配置
  3. > /usr/local/php/etc/php-fpm.conf # 清空所有内容,编辑配置文件前要把旧的内容全部清除
  4. vim /usr/local/php/etc/php-fpm.conf # 增加内容如下
  5. [global]
  6. pid = /usr/local/php/var/run/php-fpm.pid
  7. error_log = /usr/local/php/var/log/php-fpm.log
  8. [www]
  9. listen = /tmp/www.sock
  10. user = php-fpm
  11. group = php-fpm
  12. pm = dynamic
  13. pm.max_children = 50
  14. pm.start_servers = 20
  15. pm.min_spare_servers = 5
  16. pm.max_spare_servers = 35
  17. pm.max_requests = 500
  18. rlimit_files = 1024
  19. slowlog= /tmp/www slow.log # 可以排查网络慢的原因,优化网速
  20. request_slowlog_timeout = 1 # 只要执行这个脚本的时间超过1s,就要记录slow.log
  21. php_admin_value[open_basedir]=/data/www/:/tmp/ # 针对不同的域名进行不同的限制
  22. [www1]
  23. listen = /tmp/www1.sock
  24. user = php-fpm
  25. group = php-fpm
  26. pm = dynamic
  27. pm.max_children = 50
  28. pm.start_servers = 20
  29. pm.min_spare_servers = 5
  30. pm.max_spare_servers = 35
  31. pm.max_requests = 500
  32. rlimit_files = 1024
  33. # 检测是否有错
  34. usr/local/php/sbin/php-fpm -t
  35. # 重启
  36. /etc/init.d/php-fpm restart
  37. ps aux |grep fpm.conf # 查看进程,默认有20个pool
  38. ls /usr/local/nginx/conf/vhosts # 查看多个域名
  39. cat /usr/local/nginx/conf/vhosts/111.conf # 查看指定的域名

1.6 常见的502问题解决

  1. cd /usr/local/nginx/conf/vhosts/
  2. vim test.conf # 增加内容如下:
  3. server
  4. {
  5. listen 80;
  6. server_name www.test.com;
  7. index index.html index.htm index.php;
  8. root /data/www;
  9. location ~ \.php$ {
  10. include fastcgi_params;
  11. fastcgi_pass unix:/tmp/www.sock;
  12. # fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.php;
  14. fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
  15. }
  16. }
  17. # 检查配置文件是否有错
  18. /usr/local/nginx/sbin/nginx -t
  19. # 重启nginx
  20. /usr/local/nginx/sbin/nginx -s reload
  21. etc/init.d/nginx reload
  22. # 在主配置文件里找到错误日志
  23. vim ../nginx.conf
  24. # 查看错误日志,会发现/tmp/www.sock此文件权限被拒,所以网业会出现502
  25. cat /usr/local/nginx/logs/nginx_error.log #
  26. ls -l /tmp/www.sock
  27. ps aux |grep nginx
  28. # 编辑php配置文件,
  29. vim /usr/local/php/etc/php-fpm.conf #指定监听的用户与组如下
  30. [www]
  31. listen = /tmp/www.sock
  32. user = php-fpm
  33. group = php-fpm
  34. listen.owner = nobody
  35. listen.group = nobody
  36. # 检测配置文件是否有错
  37. /usr/local/php/sbin/php-fpm -t
  38. # 重启
  39. /etc/init.d/php-fpm restart

1.7 nginx用户认证

  1. cd /usr/local/nginx/conf/vhosts/
  2. htpasswd -c /usr/local/nginx/conf/.htpasswd melody # 增加加密新用户
  3. cat /usr/local/nginx/conf/.htpasswd
  4. htpasswd /usr/local/nginx/conf/.htpasswd melody1 # 增加另一个加密新用户
  5. vim test.conf # 增加内容如下:
  6. server
  7. {
  8. listen 80;
  9. server_name www.test.com
  10. index index.html index.htm index.php;
  11. root /data/www;
  12. location ~ .*admin\.php$ {
  13. auth_basic "aminglinux auth";
  14. auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
  15. include fastcgi_params;
  16. fastcgi_pass unix:/tmp/www.sock;
  17. fastcgi_index index.php;
  18. fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
  19. }
  20. location ~ \.php$ {
  21. include fastcgi_params;
  22. fastcgi_pass unix:/tmp/www.sock;
  23. # fastcgi_pass 127.0.0.1:9000;
  24. fastcgi_index index.php;
  25. fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
  26. }
  27. # 检测配置文件是否有错
  28. /usr/local/nginx/sbin/nginx -t
  29. # 重启
  30. /etc/init.d/nginx reload
  31. # 用curl检测是否解析成功
  32. curl -x127.0.0.1:80 www.test.com/admin.php
  33. curl -x127.0.0.1:80 -umelody:123456 www.test.com/admin.php # 解析带密码的

1.8 nginx域名跳转

  1. # 编辑test.conf,来设域名的跳转
  2. vim test.conf # 增加内容如下
  3. listen 80;
  4. server_name www.test.com www.aaa.com www.bbb.com;
  5. if ($host != 'www.test.com')
  6. {
  7. rewrite ^/(.*)$ http://www.test.com/$1 permanent;
  8. }
  9. # 检查nginx的配置文件是否正确
  10. /usr/local/nginx/sbin/nginx -t
  11. # 重启nginx
  12. /usr/local/nginx/sbin/nginx -s reload
  13. # 用curl检测网址是否生效
  14. curl -x127.0.0.1:80 www.aaa.com/adajaklal -I # 301跳转为www.test.com
  15. curl -x127.0.0.1:80 www.bbb.com/adajaklal -I # 301跳转为www.test.com
  16. curl -x127.0.0.1:80 www.test.com/adajaklal -I # 404没有找到,因为网址不对
  17. http://ask.apelearn.com/question/4840 # nginx的301与302如何配置的网址

flag标记可以用以下几种格式:
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301

1.9 nginx不记录指定文件类型日志

  1. # 编辑主配置文件,
  2. vim /usr/local/nginx/conf/nginx.conf 定义日志格式,把日志名称改成melody
  3. log_format melody '$remote_addr $http_x_forwarded_for [$time_local]' # 远程IP,代理IP
  4. # melody格式的日志如何用
  5. vim test.conf # 进入此配置文件
  6. access_log /tmp/access.log melody; # 首先指定目录位置
  7. # 限制gif|jpg|jpeg|png|bmp|swf这些日志不去记录
  8. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  9. {
  10. access_log off;
  11. }
  12. location ~ (static|cache)
  13. {
  14. access_log off;
  15. }
  16. # 检查nginx的配置文件是否正确
  17. /usr/local/nginx/sbin/nginx -t
  18. # 重启nginx
  19. /usr/local/nginx/sbin/nginx -s reload
  20. # 在刷新前先把旧的日志内容清空
  21. > /tmp/access.log
  22. # 查看日志内容
  23. cat /tmp/access.log

2.0 nginx日志切割

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts
  3. # 编辑配置文件
  4. vim /usr/local/sbin/nginx_logrotate.sh # 增加日志切割内容如下
  5. #!/bin/bash
  6. d=`date -d "-1 day" +%F`
  7. [ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log
  8. mv /tmp/access.log /tmp/nginx_log/$d.log
  9. /etc/init.d/nginx reload > /dev/null
  10. cd /tmp/nginx_log/
  11. gzip -f $d.log
  12. # 执行日志文件
  13. sh -x /usr/local/sbin/nginx_logrotate.sh
  14. # 查看日志文件
  15. ls /tmp/access.log
  16. cat /tmp/access.log
  17. ls /tmp/nginx_log/

2.1 nginx配置静态文件过期时间

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts/
  3. # 编辑配置文件
  4. vim test.conf # 增加静态文件过期时间内容如下
  5. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  6. {
  7. access_log off;
  8. expires 15d;
  9. }
  10. location ~ \.(js|css)
  11. {
  12. access_log off;
  13. expires 2h;
  14. }
  15. # 检查nginx的配置文件是否正确
  16. /usr/local/nginx/sbin/nginx -t
  17. # 重启nginx
  18. /usr/local/nginx/sbin/nginx -s reload

2.2 nginx配置防盗链

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts/
  3. # 编辑配置文件
  4. vim test.conf # 增加防盗链内容如下
  5. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$
  6. {
  7. access_log off;
  8. expires 15d;
  9. valid_referers none blocked *.test.com *.aaa.com;
  10. if ($invalid_referer)
  11. {
  12. return 403;
  13. }
  14. # 检查nginx的配置文件是否正确
  15. /usr/local/nginx/sbin/nginx -t
  16. # 重启
  17. /usr/local/nginx/sbin/nginx -s reload
  18. # 用curl检查 (-e显示referer)
  19. curl -e "http://www.test.com/111" -I -x127.0.0.1:80 'http:www.test.com/static/image/smiley/default/kiss.gif'

2.3 nginx访问控制

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts/
  3. # 编辑配置文件
  4. vim test.conf # 增加内容如下
  5. # 白名单允许指定的IP访问,其它的拒绝
  6. location ~ .*admin\.php$ {
  7. allow 127.0.0.1; # 白名单格式
  8. deny all;
  9. # 黑名单拒绝指定的IP访问
  10. access_log /tmp/access.log melody;
  11. deny 127.0.0.1;
  12. deny 192.168.31.0/24;
  13. # 检查nginx的配置文件是否正确
  14. /usr/local/nginx/sbin/nginx -t
  15. # 重启
  16. /usr/local/nginx/sbin/nginx -s reload
  17. # 用curl检查
  18. curl -x127.0.0.1:80 www.test.com/forum.php -I
  19. curl -x192.168.31.127:80 www.test.com/forum.php -I

2.4 nginx禁止指定user_agent

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts/
  3. # 编辑配置文件
  4. vim test.conf # 增加禁止指定user_agent的内容如下
  5. if ($http_user_agent ~* 'curl|baidu|111') # ~*同时使用,代表不区别大小写
  6. {
  7. return 403;
  8. }
  9. # 检查nginx的配置文件是否正确
  10. /usr/local/nginx/sbin/nginx -t
  11. # 重启
  12. /usr/local/nginx/sbin/nginx -s reload
  13. # 用curl检查
  14. curl -A "fkl" -x192.168.31.127:80 www.test.com/forum.php -I # 结果200,因为没有禁止的user_agent
  15. curl -A "111a" -x192.168.31.127:80 www.test.com/forum.php -I # 结果403,因为有禁止的user_agent

2.5 nginx代理详解

  1. # 进入此目录下
  2. cd /usr/local/nginx/conf/vhosts/
  3. # 配置单个百度代理
  4. vim proxy.conf # 增加内容如下
  5. server {
  6. listen 80;
  7. server_name www.baidu.com;
  8. location / {
  9. proxy_pass http://183.232.231.172/; #这里百度的IP是什么?需要PING一下
  10. #proxy_set_header Host $host;
  11. }
  12. }
  13. # PING出百度的IP
  14. ping www.baidu.com
  15. vim /etc/hosts # 删除之前写的百度IP
  16. ping www.baidu.com
  17. # 检查nginx的配置文件是否正确
  18. /usr/local/nginx/sbin/nginx -t
  19. # 重启
  20. /usr/local/nginx/sbin/nginx -s reload
  21. # 用curl检查
  22. curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度
  23. # 使用一个域名对应多个IP,相当于负载均衡(多个机器访问百度)
  24. dig www.baidu.com # 探测你的域名解析到了哪些IP
  25. yum install bind* # 安装
  26. #配置多个IP百度代理
  27. vim proxy.conf # 增加内容如下
  28. upstream melody{
  29. server 183.232.231.172:80;
  30. server 183.232.231.173:80;
  31. server {
  32. listen 80;
  33. server_name www.baidu.com;
  34. location / {
  35. proxy_pass http://melody/;
  36. proxy_set_header Host $host;
  37. }
  38. }
  39. # 检查nginx的配置文件是否正确
  40. /usr/local/nginx/sbin/nginx -t
  41. # 重启
  42. /usr/local/nginx/sbin/nginx -s reload
  43. # 用curl检查
  44. curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注