二进制源码包下载安装,为安装顺利可先使用yum安装pcre-devel;openssl; openssl-devel包javascript
cd /usr/local/srcphp
wget http://nginx.org/download/nginx-1.12.1.tar.gzcss
tar zxf nginx-1.12.1.tar.gzhtml
./configure --prefix=/usr/local/nginxjava
make &&make installnode
vim /etc/init.d/nginx #新建添加如下内容:nginx
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
chmod 755 /etc/init.d/nginx; chkconfig --add nginx ;chkconfig nginx on #设置权限和开机自启动vim
cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak #备份原有的nginx.conf文件bash
vim /usr/local/nginx/conf/nginx.conf #新建nginx.conf配置文件并添加如下内容:app
#启动nginx程序的用户 user nobody nobody; #定义子进程个数 worker_processes 2; #错误日志 error_log /usr/local/nginx/logs/nginx_error.log crit; #pid 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; #如下,每一个server对应一个虚拟主机 server { listen 80; #监听端口 server_name localhost; #域名 index index.html index.htm index.php; root /usr/local/nginx/html; #网站根目录 location ~ \.php$ #配置解析php部分 { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; #指定调用php;可写 ip:端口 形式 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
/usr/local/nginx/sbin/nginx -t #检测配置文件是否有问题
/etc/init.d/nginx start #启动nginx服务
测试php解析:
vim /usr/local/nginx/html/1.php #建立测试php文件并添加如下内容:
<?php echo "test php scripts."; ?>
curl localhost/1.php
在nginx.conf中配置server{}来定义虚拟主机并不方便管理,能够使每一个虚拟主机都有一个本身的配置文件:
vim /usr/local/nginx/conf/nginx.conf #增长如下内容:
include vhost/*.conf #表示虚拟主机配置文件将存放在/usr/local/nginx/conf/vhost中;/usr/local/nginx/conf/vhost须要手动建立
vim /usr/local/nginx/conf/vhost/default.conf #新建虚拟主机配置文件并添加如下内容:
server { listen 80 default_server; #定义监听端口;有default_server表示默认虚拟主机 server_name aaa.com; #定义域名 index index.html index.htm index.php; #定义支持的首页格式 root /data/wwwroot/default; #定义网页根目录 }
mkdir -p /data/wwwroot/default/ #建立默认虚拟主机的根目录
echo “This is a default site.”>/data/wwwroot/default/index.html #建立测试文件
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #检查并从新加载配置文件
curl localhost #测试
vim /usr/local/nginx/conf/vhost/test.com.conf #建立新的虚拟主机配置文件并添加如下内容:
server { listen 80; server_name www.test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / #表示匹配根目录 { auth_basic "auth"; #表示用户认证的名字 auth_basic_user_file /data/wwwroot/.htpasswd; #认证用户保存文件,该文件须要使用httpd的htpasswd命令生成 } }
mkdir /data/wwwroot/test.com ;echo "this is test.com" >/data/wwwroot/test.com/index.html #建立根目录和测试文件
yum install -y httpd ;htpasswd -c /usr/local/nginx/conf/htpasswd aming #指定用户aming并生成密码,-c重置
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #检查并从新加载配置文件
curl -x127.0.0.1:80 test.com -I #测试,状态码为401说明须要验证
curl -uaming:lishiming -x127.0.0.1:80 test.com #加入用户密码测试
#在server的{}中添加 location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
Apache中,servername后面只能带一个域名。而在nginx中,server_name后面能够跟多个域名
vim /usr/local/nginx/conf/vhost/test.com.conf #更改配置文件为如下内容:
server { listen 80; server_name test.com test1.com test2.com; #配置多个域名 index index.html index.htm index.php; root /data/wwwroot/test.com; #设置主域名 if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; #其余域名跳转到test.com,permanent为永久重定向,状态码为301,若是写redirect则为302 } }
/usr/local/nginx/sbin/nginx -t ;/usr/local/nginx/sbin/nginx -s reload #检查并从新加载配置文件
curl -x127.0.0.1:80 test2.com/index.html -I #测试提示301,重定向到test.com/index.html