LNMP及nginx的一些简单配置
一、Nginx 安装1、安装包组
# yum groupinstall "Development Tools" "Server Platform Deveopment"
安装出错
file /usr/share/man/man7/stappaths.7.gz from install of systemtap-devel-1.8-7.el6.x86_64
conflicts with file from package systemtap-runtime-1.8-7.el6.x86_64
需要卸载systemtap-runtime-1.8-7.el6.x86_64
# yum -y remove systemtap-runtime
2、解决依赖关系
# yum install openssl-devel pcre-devel
3、添加服务用户
# useradd -r nginx
4、编译安装
# ./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
# make && make install
5、提供服务脚本
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:- 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
Esac
6、赋予执行权限,添加服务,启动服务。
# chmod +x /etc/rc.d/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
7、重启报错
# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
解决
# mkdir -p /var/tmp/nginx/client
二、mysql通用二进制包安装
详情请看http://qiufengsong.blog./7520243/1284355
三、php安装
1、解决依赖关系
# yum -y install libmcrypt libmcrypt-devel mhash mhash-devel mcrypt libevent libevent-devel curl-devel libxml2-devel bzip2-devel
2、解压安装包,并编译安装
# tar xf php-5.4.4.tar.bz2
# cd php-5.4.4
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-
mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --
with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
# make
# make install
3、提供配置文件,服务脚本
# cp php.ini-production /etc/php.ini
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
4、编辑php-fpm的配置文件,做下简单修改,主要改pid文件
# vim /usr/local/php/etc/php-fpm.conf
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
5、启动服务
# service php-fpm start
6、服务脚本启动时没有OK字样我么可以编辑服务脚本,在其中加入这么两行
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions 脚本首行
在启动服务start的daemon后台进程前加上daemon
daemon $php_fpm_BIN --daemonize $php_opts
四、编辑配置文件,使nginx支持fastcgi,提供页面,显示phpinfo信息。
1、编辑配置文件
# /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWAREnginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL$server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
2、重启服务
1
# service php-fpm reload
3、提供web页面,编辑nginx配置文件,使得nginx首先检索index.php,打开浏览器,查看nginx是否与php构建连接。
# vim /usr/html/index.php
<?php
phpinfo();
?>
# vim /etc/nginx/nginx.conf
location / {
roothtml;
index index.php index.html index.htm;
}
启用
location ~ \.php$ {
root html;
fastcgi_pass127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
4、好了,在浏览器中输入nginx服务器的IP地址就可访问自定义的web页面了。
五、Nginx服务器为Apache服务器做代理
1、两种代理的区别
location /tmp {
proxy_pass http://172.16.5.103/;
}
location ~ /tmp {
proxy_pass http://172.16.5.103;
}
两者区别在于,前者代理到客户端服务器的根‘/’,后者代理到/tmp,而且,后者根下必须有tmp目录且后者ip后面不能有uri。此时,/tmp在代理服务器上是虚构的。
2、给报文增加报头信息
location /tmp {
proxy_pass http://172.16.5.103/; http://固定格式,不能少。
add_header X-VIA-SERVER $remote_addr;
}
$remote_addr远程客户端地址
$server_addr代理服务器地址
在浏览器中按f12,在跳出来的工具栏中选择network,点击文件的名字,在旁边可以看到报文头X-VIA-SERVER的信息
X-VIA-SERVER自定义的字符串
Connection:keep-alive
Content-Length:4
Content-Type:text/html; charset=UTF-8
Date:Sat, 14 Sep 2013 14:31:42 GMT
Server:nginx/1.4.2
X-Powered-By:PHP/5.3.3
X-VIA-SERVER:172.16.5.254远程客户端地址
3、编辑nginx配置文件,使得后端服务器可以得到远程服务器的IP地址
location /tmp {
proxy_pass http://172.16.5.103/;
add_header X-VIA-SERVER $remote_addr;
proxy_set_headerREMOTE-ADDR $remote_addr;
}
add_header报文头ip 只能是客户端和代理服务器 $remote_addr|$server_addr的ip
proxy_set_header重写报文首部,并将首部ip传递到后端服务器的日志中。
proxy_set_header设置的同时要在后端服务器编辑配置文件中日志格式的写法。
LogFormat "%{REMOTE-ADDR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
注意,在设定proxy_set_header之前,缓存记录中如果存在的是
4、直接代理和分组代理
直接代理只能为单个目录进行代理,而分组代理可以实现负载均衡。
直接代理
location /tmp {
proxy_pass http://172.16.5.101/;
}
分组代理:
upstream servers {
server 172.16.5.101 weight=2;
server 172.16.5.102 weight=3;
}
location / {
roothtml;
index index.php index.html index.htm;
proxy_pass http://servers;
add_header luliming $remote_addr;
proxy_set_header REMOTE-ADDR $remote_addr;
}
5、编辑配置文件,使客户端可以上传文件的到后端服务器
在后端apache服务器上启用DAV (httpd的扩展,也是一种网络文件系统)
<Directory "/var/www/html">
DAV on
给apache用户设置权限
# setfacl -m u:apache:rwx /var/www/html/
编辑代理服务器,使得客户端上传文件到后端服务器
location / {
root html;
index index.php index.html index.htm;
proxy_pass http://servers;
add_header luliming $remote_addr;
proxy_set_header REMOTE-ADDR $remote_addr;
if ($request_method ~* 'PUT') {
proxy_pass http://172.16.5.101;
}
}
# curl -T /etc/passwd 172.16.5.11/上传到代理服务器
<p>Resource /passwd has been created.</p>上传成功
<address>Apache/2.2.15 (Red Hat) Server at 172.16.5.101 Port 80</address>
发现上传到了后端服务器
6、nginx缓存功能
在http{}中定义缓存目录
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g;
/etc/nginx/cache 缓存目录,要在nginx代理服务器上创建该目录
levels=1:2 缓存级别 最多三级:levels=1:2:1,一级缓存,缓存的时候缓存目录下,只会创建一个目录,然后就是缓存对象,二级缓存,有两个目录,三级三个。更新缓存级别,服务要重启。每个级别所占的字节最多为2.
keys_zone=static:10m 缓存目录下索引目录的大小
inactive=24h非活动时长,24h表示全天候
max_size=1g缓存空间的大小
location /bbs {
proxy_pass http://172.16.5.101/;
proxy_cache static;
proxy_cache_valid 200 1d;
proxy_cache_valid 301 302 10m;
proxy_cache_valid any 1m;
}
proxy_cache_valid定义对应响应码的混存对象的混存时间
proxy_cache_valid 200 1d正常访问,缓存对象的缓存时间。
proxy_cache_valid 301 302 10m报文重定向,缓存对象的缓存时间。
proxy_cache_valid any 1m 其他任意响应码缓存对象的缓存时间。
再次访问代理服务器,就会在缓存目录中看到响应的缓存对象了。
离开了就不要回头,要知道这样也会再次的被刺伤 乜许忘记是能做的唯一,只要你能够幸福开心。 我也曾幻想我们的未来'可我﹑看到一片箜白'! 对呀,我对你玓爱很假。假到让人难以自信 人生是不公平的,所以不须抱怨,唯一公平的是谁都会死 我的悲伤写在脸上,连笑容也变得坚强
页:
[1]