一、进入src目录,下载源码包,在官网nginx.org下载:javascript
[root@localhost ~]# cd /usr/local/src/ [root@localhost src]# ls mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz mysql-test-5.7.18-linux-glibc2.5-x86_64.tar.gz php-5.6.32 php-5.6.32.tar.bz2 [root@localhost src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-06-10 11:50:38-- http://nginx.org/download/nginx-1.14.0.tar.gz 正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ... 正在链接 nginx.org (nginx.org)|95.211.80.227|:80... 已链接。 已发出 HTTP 请求,正在等待回应... 302 Found 位置:http://120.198.248.38/cache/nginx.org/download/nginx-1.14.0.tar.gz?ich_args2=913-10115011025885_c68489af48a342d4d96e3d5442d9961a_10001002_9c896624d1c3f3d79433518939a83798_dce717bbf01ac9d1291c27e56e2c7bd4 [跟随至新的 URL] --2018-06-10 11:50:38-- http://120.198.248.38/cache/nginx.org/download/nginx-1.14.0.tar.gz?ich_args2=913-10115011025885_c68489af48a342d4d96e3d5442d9961a_10001002_9c896624d1c3f3d79433518939a83798_dce717bbf01ac9d1291c27e56e2c7bd4 正在链接 120.198.248.38:80... 已链接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:1016272 (992K) [application/octet-stream] 正在保存至: “nginx-1.14.0.tar.gz” 100%[====================================================================================================================================>] 1,016,272 2.43MB/s 用时 0.4s 2018-06-10 11:50:38 (2.43 MB/s) - 已保存 “nginx-1.14.0.tar.gz” [1016272/1016272])
二、解压源码包:php
[root@localhost src]# tar xzvf nginx-1.14.0.tar.gz nginx-1.14.0/ nginx-1.14.0/auto/ nginx-1.14.0/conf/ nginx-1.14.0/contrib/ nginx-1.14.0/src/ nginx-1.14.0/auto/cc/msvc nginx-1.14.0/auto/cc/name nginx-1.14.0/auto/cc/owc nginx-1.14.0/auto/cc/sunc
三、进入源码包目录,配置编译文件css
//能够根据需求增长配置项,例如支持https的配置 [root@localhost src]# cd nginx-1.14.0 [root@localhost nginx-1.14.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) checking for gcc -pipe switch ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for uintptr_t ... uintptr_t found checking for PCRE JIT support ... found checking for zlib library ... found creating objs/Makefile Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@localhost nginx-1.14.0]# echo $? 0
四、编译及安装nginx:html
[root@localhost nginx-1.14.0]# make && make install make -f objs/Makefile make[1]: 进入目录“/usr/local/src/nginx-1.14.0” cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/nginx.o \ src/core/nginx.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_log.o \ src/core/ngx_log.c cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \ -o objs/src/core/ngx_palloc.o \ cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' test -d '/usr/local/nginx/html' \ || cp -R html '/usr/local/nginx' test -d '/usr/local/nginx/logs' \ || mkdir -p '/usr/local/nginx/logs' make[1]: 离开目录“/usr/local/src/nginx-1.14.0” [root@localhost nginx-1.14.0]# echo $? 0
五、安装成功,查看nginx目录结构及核心文件:java
//分别是配置文件目录、网站目录、日志目录、进程目录 [root@localhost nginx-1.14.0]# cd /usr/local/nginx/ [root@localhost nginx]# ls conf html logs sbin //核心二进制文件 [root@localhost nginx]# ls -l sbin/nginx -rwxr-xr-x 1 root root 3746616 6月 10 11:57 sbin/nginx
六、修改、检查配置文件:node
//用本身的配置文件,不用自带的 [root@localhost nginx]# mv conf/nginx.conf conf/nginx.conf.bak [root@localhost nginx]# cd conf/ [root@localhost conf]# ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf.bak scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@localhost conf]# vim 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; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } } [root@localhost conf]# /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
七、编辑启动脚本:mysql
[root@localhost conf]# vim /etc/init.d/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 //修改成755权限 [root@localhost conf]# chmod 755 /etc/init.d/nginx [root@localhost conf]# ls -l !$ ls -l /etc/init.d/nginx -rwxr-xr-x 1 root root 1133 6月 10 13:08 /etc/init.d/nginx
八、加入开机启动列表,设置开机启动:linux
[root@localhost conf]# chkconfig --add nginx [root@localhost conf]# chkconfig nginx on
九、启动nginx服务:nginx
[root@localhost conf]# /etc/init.d/n netconsole network nginx [root@localhost conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
十、查看进程:访问主页验证sql
[root@localhost conf]# ps aux |grep nginx root 4096 0.0 0.0 20548 628 ? Ss 13:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4097 0.0 0.1 22992 3216 ? S 13:10 0:00 nginx: worker process nobody 4098 0.0 0.1 22992 3216 ? S 13:10 0:00 nginx: worker process [root@localhost conf]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4096/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 897/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1256/master tcp6 0 0 :::22 :::* LISTEN 897/sshd tcp6 0 0 ::1:25 :::* LISTEN 1256/master tcp6 0 0 :::3306 :::* LISTEN 1138/mysqld //200状态码,访问正常 [root@localhost conf]# curl localhost -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 05:11:57 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Sun, 10 Jun 2018 03:57:01 GMT Connection: keep-alive ETag: "5b1ca18d-264" Accept-Ranges: bytes
一、把主配置文件下的server段注释或删除掉,增长include段
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf http { # server # { # listen 80; # server_name localhost; # index index.html index.htm index.php; # root /usr/local/nginx/html; # location ~ \.php$ # { # include fastcgi_params; # fastcgi_pass unix:/tmp/php-fcgi.sock; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; # } # } include vhost/*.conf; //指定虚拟主机配置文件目录在vhost/ 下 }
二、建立虚拟主机配置文件,一个虚拟主机就有一个配置文件。
[root@localhost conf]# mkdir vhost/ [root@localhost conf]# cd vhost/ [root@localhost vhost]# ls [root@localhost vhost]# vim aaa.com.conf //aaa.com网站的虚拟主机配置文件 server { listen 80 default_server; //有default_server标志的,表明是默认虚拟主机,只要访问没有指定的域名过来,就会默认访问aaa.com虚拟主机 server_name aaa.com; index index.html index.php; root /data/wwwroot/aaa.com; }
三、检查配置文件、从新加载配置文件:
[root@localhost conf]# /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 [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
四、建立网站目录,进行访问验证:
[root@localhost ~]# mkdir -p /data/wwwroot/aaa.com/ [root@localhost ~]# cd !$ cd /data/wwwroot/aaa.com/ [root@localhost aaa.com]# ls [root@localhost aaa.com]# vim index.html [root@localhost aaa.com]# curl -x127.0.0.1:80 bbb.com echo "this is nginx testpage!" [root@localhost aaa.com]# curl -x127.0.0.1:80 bbb.com -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 05:54:50 GMT Content-Type: text/html Content-Length: 31 Last-Modified: Sun, 10 Jun 2018 05:54:34 GMT //200状态码,由于是默认的虚拟主机,因此其余域名bbb.com也能访问过来。
一、在vhost目录下载增长另一个虚拟主机配置文件
[root@localhost ~]# cp /usr/local/nginx/conf/vhost/aaa.com.conf /usr/local/nginx/conf/vhost/test.com.conf
二、设置虚拟主机配置文件,加入用户认证配置段
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } [root@localhost ~]# /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 [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
三、用apache的htpasswd工具,生成密码文件
//可单独安装htpasswd工具 [root@localhost ~]# yum install -y httpd 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * epel: mirrors.tongji.edu.cn * extras: mirrors.163.com * updates: mirrors.163.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 httpd.x86_64.0.2.4.6-80.el7.centos 将被 安装 --> 正在处理依赖关系 httpd-tools = 2.4.6-80.el7.centos,它被软件包 httpd-2.4.6-80.el7.centos.x86_64 须要 --> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-80.el7.centos.x86_64 须要 --> 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-80.el7.centos.x86_64 须要 --> 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-80.el7.centos.x86_64 须要 --> 正在检查事务 ---> 软件包 apr.x86_64.0.1.4.8-3.el7_4.1 将被 安装 ---> 软件包 apr-util.x86_64.0.1.5.2-6.el7 将被 安装 ---> 软件包 httpd-tools.x86_64.0.2.4.6-80.el7.centos 将被 安装 ---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================================================================================================================================== Package 架构 版本 源 大小 ============================================================================================================================================================================== 正在安装: httpd x86_64 2.4.6-80.el7.centos base 2.7 M 为依赖而安装: apr x86_64 1.4.8-3.el7_4.1 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-80.el7.centos base 89 k mailcap noarch 2.1.41-2.el7 base 31 k 事务概要 ============================================================================================================================================================================== 安装 1 软件包 (+4 依赖软件包) 总下载量:3.0 M 安装大小:10 M Downloading packages: (1/5): apr-1.4.8-3.el7_4.1.x86_64.rpm | 103 kB 00:00:00 (2/5): apr-util-1.5.2-6.el7.x86_64.rpm | 92 kB 00:00:00 (3/5): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00 (4/5): httpd-2.4.6-80.el7.centos.x86_64.rpm | 2.7 MB 00:00:07 (5/5): httpd-tools-2.4.6-80.el7.centos.x86_64.rpm | 89 kB 00:00:15 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 总计 192 kB/s | 3.0 MB 00:00:16 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : apr-1.4.8-3.el7_4.1.x86_64 1/5 正在安装 : apr-util-1.5.2-6.el7.x86_64 2/5 正在安装 : httpd-tools-2.4.6-80.el7.centos.x86_64 3/5 正在安装 : mailcap-2.1.41-2.el7.noarch 4/5 正在安装 : httpd-2.4.6-80.el7.centos.x86_64 5/5 验证中 : httpd-tools-2.4.6-80.el7.centos.x86_64 1/5 验证中 : apr-1.4.8-3.el7_4.1.x86_64 2/5 验证中 : mailcap-2.1.41-2.el7.noarch 3/5 验证中 : httpd-2.4.6-80.el7.centos.x86_64 4/5 验证中 : apr-util-1.5.2-6.el7.x86_64 5/5 已安装: httpd.x86_64 0:2.4.6-80.el7.centos 做为依赖被安装: apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7 完毕! //lgs为用户名,回车后输入两次密码,设置成功 [root@localhost ~]# htpasswd -c /usr/local/nginx/conf/htpasswd lgs New password: Re-type new password: Adding password for user lgs [root@localhost ~]# cat /usr/local/nginx/conf/htpasswd lgs:$apr1$/XnF945H$/hM8VWuy4u1eNDmchA39s.
四、验证用户认证:
[root@localhost ~]# mkdir -p /data/wwwroot/test.com/ [root@localhost ~]# vim /data/wwwroot/test.com/1.txt //报401错误,提示须要认证 [root@localhost ~]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 06:15:56 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"
一、设置虚拟主机配置文件,加入域名重定向配置段
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com test1.com test2.com; //支持多个域名,与apache不一样,因此要域名重定向,跳转到test.com上,不影响他的搜索权重 index index.html index.php; root /data/wwwroot/test.com; # location / # { # auth_basic "Auth"; # auth_basic_user_file /usr/local/nginx/conf/htpasswd; # } if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; } //使用rewrite模块实现。redirect是临时重定向,302状态码。 } [root@localhost ~]# /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 [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
二、验证重定向:
[root@localhost ~]# curl -x127.0.0.1:80 test2.com/1.txt -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.0 Date: Sun, 10 Jun 2018 06:26:13 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/1.txt //301状态码,重定向到test.com去。