@Great-Chinese
2016-12-05T08:13:21.000000Z
字数 11367
阅读 1142
LNMP架构搭建与优化
A:下载与解压
wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2tar jxvf php-5.4.37.tar.bz2
B:配置编译参数
cd php-5.4.37 # 进入此目录中./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:编译
makeecho $?
D:安装
rm -rf /usr/local/php/ # 在安装前,要把之前旧的php目录删除掉make installecho $?
cp php.ini-production /usr/local/php/etc/php.ini # 拷贝生产环境的配置文件cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 拷贝启动脚本vim /etc/init.d/php-fpm # 编辑启动的shell脚本chmod 755 /etc/init.d/php-fpm # 授予执行权限cd /usr/local/php/etc/mv php-fpm.conf.default php-fpm.confchkconfig --add php-fpm # 加入系统服务列表中chkconfig php-fpm on # 开机启动useradd -s /sbin/nologin php-fpm # 增加用户service php-fpm start # 启动/usr/local/php/sbin/php-fpm -t # 检查是否有问题ps aux|grep php-fpmnetstat -lnp
A:下载与解压
cd /usr/local/src # 进入此目录中wget http://nginx.org/download/nginx-1.6.2.tar.gztar zxvf nginx-1.6.2.tar.gz
B:配置编译参数
cd nginx-1.6.2 # 进入此目录中./configure --prefix=/usr/local/nginx --with-pcreyum install -y pcre-devel
C:编译/安装
makemake install
cd /usr/local/nginx/usr/local/apache2/bin/apachectl stop/usr/local/nginx/sbin/nginx # 启动nginxps aux |grep nginxnetstat lnp
# 打开nginx默认的配置文件vim /usr/local/nginx/conf/nginx.conf # 打开下面的内容location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;include fastcgi_params;}cat /usr/local/nginx/html/index.htmlcd /usr/local/nginx/html/ #进入html目录vi info.php # 然后编辑info文件/usr/local/nginx/sbin/nginx -t # 检查是否有错/usr/local/nginx/sbin/nginx -s reload # 重启后,在网址上输入IP,就可以解析php了curl localhostcurl localhost/info.php
A:编写nginx启动脚本,并加入系统服务
vim /etc/init.d/nginx # 加入以下内容#!/bin/bash# chkconfig: - 30 21# description: http service.# Source Function Library. /etc/init.d/functions# Nginx SettingsNGINX_SBIN="/usr/local/nginx/sbin/nginx"NGINX_CONF="/usr/local/nginx/conf/nginx.conf"NGINX_PID="/usr/local/nginx/logs/nginx.pid"RETVAL=0prog="Nginx"start() {echo -n $"Starting $prog: "mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL}stop() {echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL}reload(){echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL}restart(){stopstart}configtest(){$NGINX_SBIN -c $NGINX_CONF -treturn 0}case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1esacexit $RETVAL
B:保存后,更改权限:
chmod 755 /etc/init.d/nginxchkconfig --add nginx # 加到启动列表中chkconfig nginx on # 开机启动,执行此条命令
C: nginx的启动,重启,停止,检测
service nginx startservice nginx restartservice nginx stopservice nginx configtest
D:更改nginx配置
vim /usr/local/nginx/conf/nginx.conf #增加以下内容user nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{use epoll;worker_connections 6000;}http{include mime.types;default_type application/octet-stream;server_names_hash_bucket_size 3526;server_names_hash_max_size 4096;log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]''$host "$request_uri" $status''"$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;request_pool_size 4k;output_buffers 4 32k;postpone_output 1460;client_max_body_size 10m;client_body_buffer_size 256k;client_body_temp_path /usr/local/nginx/client_body_temp;proxy_temp_path /usr/local/nginx/proxy_temp;fastcgi_temp_path /usr/local/nginx/fastcgi_temp;fastcgi_intercept_errors on;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_buffers 4 8k;gzip_comp_level 5;gzip_http_version 1.1;gzip_types text/plain application/x-javascript text/css text/htm application/xml;include vhosts/*.conf;}
cd /usr/local/nginx/confmkdir vhostscd vhostsmkdir /tmp/1233# 创建虚拟主机配置文件vim default.conf # 增加内容如下server{listen 80 default_server;server_name localhost;index index.html index.htm index.php;root /tmp/1233;deny all;}/usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错/etc/init.d/nginx reload # 加载配置文件curl -x127.0.0.1:80 www.baidu.com# 创建新的虚拟主机配置文件vim 111.conf # 增加内容如下server{listen 80;server_name 111.com;index index.html index.htm index.php;root /data/www;location ~ \.php$ {include fastcgi_params;# fastcgi_pass unix:/tmp/php-fcgi.sock;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;}}/usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错/etc/init.d/nginx reload # 加载配置文件curl -x127.0.0.1:80 111.com/forum.php -I
/usr/local/php/etc/php-fpm.conf # 管理php-fpm相关服务的配置/usr/local/php/etc/php.ini # php的全局配置> /usr/local/php/etc/php-fpm.conf # 清空所有内容,编辑配置文件前要把旧的内容全部清除vim /usr/local/php/etc/php-fpm.conf # 增加内容如下[global]pid = /usr/local/php/var/run/php-fpm.piderror_log = /usr/local/php/var/log/php-fpm.log[www]listen = /tmp/www.sockuser = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024slowlog= /tmp/www slow.log # 可以排查网络慢的原因,优化网速request_slowlog_timeout = 1 # 只要执行这个脚本的时间超过1s,就要记录slow.logphp_admin_value[open_basedir]=/data/www/:/tmp/ # 针对不同的域名进行不同的限制[www1]listen = /tmp/www1.sockuser = php-fpmgroup = php-fpmpm = dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024# 检测是否有错usr/local/php/sbin/php-fpm -t# 重启/etc/init.d/php-fpm restartps aux |grep fpm.conf # 查看进程,默认有20个poolls /usr/local/nginx/conf/vhosts # 查看多个域名cat /usr/local/nginx/conf/vhosts/111.conf # 查看指定的域名
cd /usr/local/nginx/conf/vhosts/vim test.conf # 增加内容如下:server{listen 80;server_name www.test.com;index index.html index.htm index.php;root /data/www;location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/tmp/www.sock;# fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;}}# 检查配置文件是否有错/usr/local/nginx/sbin/nginx -t# 重启nginx/usr/local/nginx/sbin/nginx -s reloadetc/init.d/nginx reload# 在主配置文件里找到错误日志vim ../nginx.conf# 查看错误日志,会发现/tmp/www.sock此文件权限被拒,所以网业会出现502cat /usr/local/nginx/logs/nginx_error.log #ls -l /tmp/www.sockps aux |grep nginx# 编辑php配置文件,vim /usr/local/php/etc/php-fpm.conf #指定监听的用户与组如下[www]listen = /tmp/www.sockuser = php-fpmgroup = php-fpmlisten.owner = nobodylisten.group = nobody# 检测配置文件是否有错/usr/local/php/sbin/php-fpm -t# 重启/etc/init.d/php-fpm restart
cd /usr/local/nginx/conf/vhosts/htpasswd -c /usr/local/nginx/conf/.htpasswd melody # 增加加密新用户cat /usr/local/nginx/conf/.htpasswdhtpasswd /usr/local/nginx/conf/.htpasswd melody1 # 增加另一个加密新用户vim test.conf # 增加内容如下:server{listen 80;server_name www.test.comindex index.html index.htm index.php;root /data/www;location ~ .*admin\.php$ {auth_basic "aminglinux auth";auth_basic_user_file /usr/local/nginx/conf/.htpasswd;include fastcgi_params;fastcgi_pass unix:/tmp/www.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;}location ~ \.php$ {include fastcgi_params;fastcgi_pass unix:/tmp/www.sock;# fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;}}# 检测配置文件是否有错/usr/local/nginx/sbin/nginx -t# 重启/etc/init.d/nginx reload# 用curl检测是否解析成功curl -x127.0.0.1:80 www.test.com/admin.phpcurl -x127.0.0.1:80 -umelody:123456 www.test.com/admin.php # 解析带密码的
# 编辑test.conf,来设域名的跳转vim test.conf # 增加内容如下listen 80;server_name www.test.com www.aaa.com www.bbb.com;if ($host != 'www.test.com'){rewrite ^/(.*)$ http://www.test.com/$1 permanent;}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启nginx/usr/local/nginx/sbin/nginx -s reload# 用curl检测网址是否生效curl -x127.0.0.1:80 www.aaa.com/adajaklal -I # 301跳转为www.test.comcurl -x127.0.0.1:80 www.bbb.com/adajaklal -I # 301跳转为www.test.comcurl -x127.0.0.1:80 www.test.com/adajaklal -I # 404没有找到,因为网址不对http://ask.apelearn.com/question/4840 # nginx的301与302如何配置的网址
flag标记可以用以下几种格式:
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
# 编辑主配置文件,vim /usr/local/nginx/conf/nginx.conf 定义日志格式,把日志名称改成melodylog_format melody '$remote_addr $http_x_forwarded_for [$time_local]' # 远程IP,代理IP# melody格式的日志如何用vim test.conf # 进入此配置文件access_log /tmp/access.log melody; # 首先指定目录位置# 限制gif|jpg|jpeg|png|bmp|swf这些日志不去记录location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${access_log off;}location ~ (static|cache){access_log off;}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启nginx/usr/local/nginx/sbin/nginx -s reload# 在刷新前先把旧的日志内容清空> /tmp/access.log# 查看日志内容cat /tmp/access.log
# 进入此目录下cd /usr/local/nginx/conf/vhosts# 编辑配置文件vim /usr/local/sbin/nginx_logrotate.sh # 增加日志切割内容如下#!/bin/bashd=`date -d "-1 day" +%F`[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_logmv /tmp/access.log /tmp/nginx_log/$d.log/etc/init.d/nginx reload > /dev/nullcd /tmp/nginx_log/gzip -f $d.log# 执行日志文件sh -x /usr/local/sbin/nginx_logrotate.sh# 查看日志文件ls /tmp/access.logcat /tmp/access.logls /tmp/nginx_log/
# 进入此目录下cd /usr/local/nginx/conf/vhosts/# 编辑配置文件vim test.conf # 增加静态文件过期时间内容如下location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${access_log off;expires 15d;}location ~ \.(js|css){access_log off;expires 2h;}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启nginx/usr/local/nginx/sbin/nginx -s reload
# 进入此目录下cd /usr/local/nginx/conf/vhosts/# 编辑配置文件vim test.conf # 增加防盗链内容如下location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)${access_log off;expires 15d;valid_referers none blocked *.test.com *.aaa.com;if ($invalid_referer){return 403;}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启/usr/local/nginx/sbin/nginx -s reload# 用curl检查 (-e显示referer)curl -e "http://www.test.com/111" -I -x127.0.0.1:80 'http:www.test.com/static/image/smiley/default/kiss.gif'
# 进入此目录下cd /usr/local/nginx/conf/vhosts/# 编辑配置文件vim test.conf # 增加内容如下# 白名单允许指定的IP访问,其它的拒绝location ~ .*admin\.php$ {allow 127.0.0.1; # 白名单格式deny all;# 黑名单拒绝指定的IP访问access_log /tmp/access.log melody;deny 127.0.0.1;deny 192.168.31.0/24;# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启/usr/local/nginx/sbin/nginx -s reload# 用curl检查curl -x127.0.0.1:80 www.test.com/forum.php -Icurl -x192.168.31.127:80 www.test.com/forum.php -I
# 进入此目录下cd /usr/local/nginx/conf/vhosts/# 编辑配置文件vim test.conf # 增加禁止指定user_agent的内容如下if ($http_user_agent ~* 'curl|baidu|111') # ~*同时使用,代表不区别大小写{return 403;}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启/usr/local/nginx/sbin/nginx -s reload# 用curl检查curl -A "fkl" -x192.168.31.127:80 www.test.com/forum.php -I # 结果200,因为没有禁止的user_agentcurl -A "111a" -x192.168.31.127:80 www.test.com/forum.php -I # 结果403,因为有禁止的user_agent
# 进入此目录下cd /usr/local/nginx/conf/vhosts/# 配置单个百度代理vim proxy.conf # 增加内容如下server {listen 80;server_name www.baidu.com;location / {proxy_pass http://183.232.231.172/; #这里百度的IP是什么?需要PING一下#proxy_set_header Host $host;}}# PING出百度的IPping www.baidu.comvim /etc/hosts # 删除之前写的百度IPping www.baidu.com# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启/usr/local/nginx/sbin/nginx -s reload# 用curl检查curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度# 使用一个域名对应多个IP,相当于负载均衡(多个机器访问百度)dig www.baidu.com # 探测你的域名解析到了哪些IPyum install bind* # 安装#配置多个IP百度代理vim proxy.conf # 增加内容如下upstream melody{server 183.232.231.172:80;server 183.232.231.173:80;}server {listen 80;server_name www.baidu.com;location / {proxy_pass http://melody/;proxy_set_header Host $host;}}# 检查nginx的配置文件是否正确/usr/local/nginx/sbin/nginx -t# 重启/usr/local/nginx/sbin/nginx -s reload# 用curl检查curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度