6月7日

12.6 Nginx安装


1. 进入存放源码包目录

cd /usr/local/src/

2. 下载源码包

 wget http://nginx.org/download/nginx-1.12.1.tar.gz

3. 解压压缩包

tar zxvf nginx-1.12.1.tar.gz

4. 安装nginx

cd nginx-1.12.1

 ./configure --prefix=/usr/local/nginx

5. 编译nginx

make

make install

ls /usr/local/nginx/    //查看nginx核心配置文件

blob.png 

6. 创建nginx配置文件及编辑启动脚本

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

7.修改启动脚本权限

chmod 755 /etc/init.d/nginx

8. 添加nginx服务

 chkconfig --add nginx

9. 设置开机启动

chkconfig nginx on

chkconfig --list

blob.png 

10. 配置nginx的配置文件

cd /usr/local/nginx/conf/

blob.png 

进入nginx配置文件目录,将原nginx.conf 文件备份,命名为后缀.bak

mv nginx.conf nginx.conf.bak

vim nginx.conf

新建并添加如下内容到配置文件中:写入如下内容(参考  https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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;         }         } }

11. 测试配置文件语法

/usr/local/nginx/sbin/nginx -t

blob.png 

12. 开启Nginx服务

/etc/init.d/nginx start

blob.png 

13.查看nginx服务进程和端口是否启动

 ps aux |grep nginx

 netstat -lntp |grep 80

blob.png 

14.测试Nginx解析php

ls /usr/local/nginx/html/  //页面文件目录

blob.png 

vim /usr/local/nginx/html/1.php

配置如下内容: <?php echo "This is nginx test page."; ?>

 

14.1 使用curl测试

curl localhost/1.php

blob.png 

 

12.7 默认虚拟主机

Nginx默认虚拟主机

Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机,但和httpd不相同的地方是,它还有一个配置用来标记默认虚拟主机,也就是说,如果没有这个标记,第一个虚拟主机为默认虚拟主机。

1.. 编辑配置文件:

vim /usr/local/nginx/conf/nginx.conf

增加以下内容: include vhost/*.conf

如下图

blob.png 

2. 创建一个vhost目录

 mkdir /usr/local/nginx/conf/vhost

 cd /usr/local/nginx/conf/vhost/

 vim aaa.conf    //进入vhost目录下并创建编辑一个.conf文件

增加如下内容: server {     listen 80 default_server;  // 有这个标记的就是默认虚拟主机     server_name aaa.com;     index index.html index.htm index.php;     root /data/wwwroot/default; }

blob.png 

3.创建default目录

mkdir -p /data/wwwroot/default/

cd /data/wwwroot/default/

vim index.html

default目录下的index.html文件中定义如下内容:

This is the default site.

4.测试语法,重新加载配置文件(不需要重启服务)

 /usr/local/nginx/sbin/nginx -t

 /usr/local/nginx/sbin/nginx -s reload //重新加载配置

blob.png 

5.使用curl测试

curl localhost

 curl -x127.0.0.1:80 123.com

curl -x127.0.0.1:80 ddd.com

blob.png 

解释说明:

访问的域名无论是指定的aaa.com还是其它域名,只要解析过来,指向到我们服务器,都能访问到这个站点,这就是默认虚拟主机。

12.8 Nginx用户认证.

1. 创建一个虚拟主机配置文件

vim /usr/local/nginx/conf/vhost/test.com.conf

blob.png 

2.创建生成密码文件

 yum install -y httpd      //为了可以使用Apachehtpasswd工具创建用户

htpasswd -c /usr/local/nginx/conf/htpasswd aming  

htpasswd  /usr/local/nginx/conf/htpasswd cfk

(如果还要生成第二个用户密码文件,把-c去掉即可,如果不去掉的话就是重置密码的意思)

blob.png 

3.测试语法及重新加载配置

 /usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

blob.png 

4.使用curl命令测试用户认证

curl -x127.0.0.1:80 test.com

 curl -x127.0.0.1:80 test.com -I

blob.png 

curl -x127.0.0.1:80 -uaming:123123 test.com -I  //加用户访问,指定用户和密码

mkdir /data/wwwroot/test.com                   //创建用户目录

echo “test.com”>/data/wwwroot/test.com/index.html     //test.com目录下编辑index.html

curl -x127.0.0.1:80 -uaming:aming test.com

blob.png 

5.针对目录的用户认证

vim /usr/local/nginx/conf/vhost/test.com.conf

进入配置文件,在location面加上目录名字就可以

blob.png 

5.1测试语法及重新加载配置

 /usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

5.2 使用curl测试:

curl -x127.0.0.1:80 test.com   //访问网站正常

curl -x127.0.0.1:80 test.com/admin    //访问admin目录出现401,需要认证

blob.png 

 curl -x127.0.0.1:80 -uaming:123123   test.com/admin/   //使用用户密码访问正常

blob.png 

 12.9 Nginx域名重定向

1, 更改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;

    }

}

最终配置

blob.png 

//server_name后面支持写多个域名,这里要和httpd的做一个对比

//permanent为永久重定向,状态码为301,如果写redirect则为302

2. 测试语法及重新加载配置

 /usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

3.使用curl测试

curl -x127.0.0.1:80 test2.com/index.html -I   //访问test2.com后会跳转到test.com

blob.png 

curl -x127.0.0.1:80 test2.com/index.html/adgagadga -I  //访问test2.com后会跳转到test.com

blob.png 

 curl -x127.0.0.1:80 test3.com/index.html -I  //访问test3.com就跳转到默认虚拟主机,

blob.png 

 





猜你喜欢

转载自blog.51cto.com/404006045/2126191