安装nginx
- 创建相关用户和目录创建www用户和组
/usr/sbin/groupadd www; /usr/sbin/useradd -g www www; mkdir -p /home/www/htdocs; chmod a+w /home/www/htdocs; chown -R www:www /home/www/; #命令汇总: /usr/sbin/groupadd www;/usr/sbin/useradd -g www www;mkdir -p /home/www/htdocs;chmod a+w /home/www/htdocs;chown -R www:www /home/www/
- 创建nginx日志目录
mkdir -p /home/www/logs; chmod a+w /home/www/logs; chown -R www:www /home/www/logs; #命令汇总: mkdir -p /home/www/logs;chmod a+w /home/www/logs;chown -R www:www /home/www/logs
-
- 安装nginx必需的库:gcc zlib-devel openssl-devel pcre
#yum 安装 gcc zlib-devel openssl-devel yum -y install gcc gcc-c++ zlib-devel openssl-devel;
- 手动安装pcre库,为了使nginx支持HTTP Rewrite 模块
pcre下载:http://sourceforge.net/projects/pcre/files/pcre/wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz; tar -zxvf pcre-8.37.tar.gz; cd pcre-8.20/; ./configure; make && make install #命令汇总: wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz;tar -zxvf pcre-8.37.tar.gz;cd pcre-8.37/;./configure;make && make install;cd ../
建立相关库的软连接
#64位 ln -sf /usr/local/lib/libpcre.so /usr/lib64/libpcre.so ln -sf /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1 ln -sf /usr/local/lib/libpcre.so.1.2.5 /usr/lib64/libpcre.so.1.2.5 ldconfig
- 安装nginx必需的库:gcc zlib-devel openssl-devel pcre
- 安装基本nginx
tar -zxvf nginx-1.1.19.tar.gz; cd nginx-1.1.19/; ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module; make && make install #命令汇总: tar -zxvf nginx-1.1.19.tar.gz;cd nginx-1.1.19/;./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module;make && make install;cd ../ #--with-http_stub_status_module 用来监控nginx的当前状态
- Nginx vhost 域名配置方式
nginx.conf整体配置: vi /usr/local/nginx/conf/nginx.conf #输入以下内容: # nginx conf conf/nginx.conf # Created by chenai # Last Updated 2012.06.01 user www www; worker_processes 2; error_log logs/error.log notice; pid logs/nginx.pid; worker_rlimit_nofile 5120; events { use epoll; worker_connections 5120; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 800m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #在nginx.conf文件中的http 里加入 autoindex on; #显示目录 autoindex_exact_size on; #显示文件大小 默认为on,显示出文件的确切大小,单位是bytes 改为off后,显示出文件的大概大小,单位是kB或者MB或者GB autoindex_localtime on; #显示文件时间 默认为off,显示的文件时间为GMT时间 改为on后,显示的文件时间为文件的服务器时间 #日志 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; include vhost/*.conf; }
Nginx vhost 多域名配置方式
# mkdir /usr/local/nginx/conf/vhost # vim /usr/local/nginx/conf/vhost/localhost.conf #配置: server { listen 80; server_name localhost; root /home/www/htdocs; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } #阻止svn/git泄露 location ~ /\.(ht|svn|git) { deny all; } #日志 access_log logs/localhost.log main; }
- 启动nginx 检查nginx配置是否正确,出现以下类似信息表示配置正确
# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- 查看nginx的编译选项
# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.1.19 built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
- nginx启动脚本
vim /etc/init.d/nginxd #!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/nginx.pid # admin chenai # Last Updated 20120.6.1 # 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/local/nginx/sbin/nginx" prog=$(basename $nginx) #必填 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo #service php-fpm start [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " $nginx -s stop echo_success retval=$? echo #service php-fpm stop [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " $nginx -s reload RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } version() { $nginx -V } 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|version) $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|version}" exit 2 esac #增加启动权限 chmod a+x /etc/init.d/nginxd
为方便启动nginx,设置变量
# vi .bashrc alias sernginx='service nginxd' #配置生效 source .bashrc
开启php-fpm 和 nginx
[root@localhost conf]# serfpm restart 停止 php-fpm: [确定] 正在启动 php-fpm: [确定] [root@localhost conf]# sernginx restart 停止 nginx: [确定] 正在启动 nginx: [确定]
-
编写每天定时切割Nginx日志的脚本
-
创建脚本
vim /root/runscript/cut_nginx_log.sh #!/bin/bash # nginx日志切割脚本 # This script run at 00:00 #设置日志文件存放目录(必填) logs_path="/usr/local/nginx/logs/" #nginx(必填) nginx="/usr/local/nginx/sbin/nginx" #第一种日志生成方式:按照目录如2013/07/localhost_20130725.log mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}localhost.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/localhost_$(date -d "yesterday" +"%Y%m%d").log #第二种日志生成方式:直接生成在logs_path目录下 mv ${logs_path}access.log ${logs_path}localhost_$(date -d "yesterday" +"%Y%m%d").log #nginx restart $nginx -s reload
-
设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e #nginx 日志切割 00 00 * * * /bin/bash /root/runscript/cut_nginx_log.sh #重启cron service crond restart
-