LNMP架构(二)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一  Nginx安装

1、切换目录

    # cd /usr/local/src

2、下载

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

3、解压

    # tar xzvf nginx-1.12.1.tar.gz

4、切换到nginx目录下

    # cd nginx-1.12.1/

110754_dZFx_3746774.png

5、编译

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

以上编译参数是一般情况下使用,在实际编译时,需要根据需求添加参数,比如网站添加了ssl证书时,就需要将ssl相关的模块给nginx配置上,因此我们需要注意后续将下载的源码包保留,可能后续会用到里面包含的某个模块

6、make && make install

    # make && make install

安装完成后,我们来看下/usr/local/nginx目录下的文件

111706_UjLE_3746774.png

其中conf下是nginx的配置文件

111742_GGJN_3746774.png

html下是样例文件

111804_ABcj_3746774.png

logs目录存放日志文件,sbin下面是进程,也就是核心文件

111844_VKnE_3746774.png

支持 -t检查配置文件语法

112027_Lv1z_3746774.png

7、编辑启动脚本

    启动脚本需要放在/etc/init.d/目录下

    # vim /etc/init.d/nginx       //创建并编辑启动脚本

    在启动脚本中加入以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/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

8、修改启动脚本权限

    # chmod 755 /etc/init.d/nginx

9、将nginx加入启动服务列表

    # chkconfig --add nginx

10、编辑配置文件

    此时/usr/local/nginx/conf/目录下已经有一个nginx.conf配置文件,

113647_UDYw_3746774.png

但是我们不用这个配置文件,我们用自己写的配置文件,所有我们先将原来的nginx.conf备份

    # mv nginx.conf nginx.conf.bak

113835_SteV_3746774.png

接下来创建并编辑我们自己的nginx.conf配置文件

    # vim nginx.conf

在配置文件中加入以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf):

user nobody nobody;  //user用于定义启动nginx的是哪个用户,也就是如果要让nginx去做读写操作时是使用谁的身份去操作的

worker_processes 2;  //定义子进程有几个

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;    //定义nginx最多可以打开多少个文件

events

{

use epoll;   //使用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;  //nginx监听的端口

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-fpm监听的端口或socket,可替代为fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}

}

}

11、检查配置文件语法错误

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

170406_T5Kb_3746774.png

12、启动

    # /etc/init.d/nginx start    //启动前应确认apache服务已经关闭,如果没有关闭,出现如下现象

172224_Z4u9_3746774.png

13、查看nginx是否成功启动

    # ps aux |grep nginx    //下图中Ss表示此进程为父进程,下面为2个worker process子进程,一般父进程属主都是root,子进程属主为上面我们配置的nobody

172652_a62a_3746774.png

    # ps aux |grep php-fpm   //同样,php-fpm父进程属主都是root,子进程属主为上面我们配置的php-fpm

172719_peHs_3746774.png

14、测试访问默认页面

    使用下面这两组命令访问的结果是一样的

    # curl localhost

180657_Y7zw_3746774.png

    # curl 127.0.0.1:80 

180556_2eWy_3746774.png

上图中访问到的页面实际上就是我们在配置文件中配置的第一个虚拟主机(也是默认虚拟主机)设置的默认访问页面/usr/local/nginx/html/index.html

181044_oP3y_3746774.png

进一步测试,我们在/usr/local/nginx/html/目录下写一个1.php的文件,看是否能成功解析,1.php中写入以下内容:

    <?php

    echo "This is nginx test page!";

    ?>

保存后,我们来访问这个页面

    # curl localhost/1.php  //下面是访问成功截图

180354_moBR_3746774.png

 

二 默认虚拟主机

1、修改配置文件

    去掉下图框中内容:

184726_2YuC_3746774.png

    在倒数第二行增加如下内容:

        include vhost/*.conf;   //表示虚拟主机配置文件放在当前目录下的子目录vhost中;

185405_o7Q3_3746774.png

2、创建虚拟主机配置文件存放目录

    # mkdir vhost    //当前目录为/usr/local/nginx/conf/

3、创建并编辑虚拟主机配置文件

    # vim vhost/aaa.com.conf     

    在文件中写入以下内容:

server

{    

    listen 80 default_server;  // 有default_server这个标记的就是默认虚拟主机    

    server_name aaa.com;    

    index index.html index.htm index.php;     //指定索引页

    root /data/wwwroot/default;   //网站所在目录

}

4、创建网站根目录

    如果网站指定的目录还不存在的话,需要创建这个目录

    # mkdir /data/wwwroot/default

5、在网站目录下写一个页面

    # cd /data/wwwroot/default

    # vim index.html

    文件中写入以下内容:

   191919_8l3a_3746774.png

6、测试nginx配置文件是否有语法错误

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

7、重新加载配置文件

    # /etc/init.d/nginx restart      //此命令直接通过重启nginx来重新加载配置文件

    # /usr/local/nginx/sbin/nginx -s reload     //此命令不用重启nginx服务就可以重新加载配置文件

8、测试访问我们写的页面

    # curl localhost

192647_aGd7_3746774.png

192929_TuPM_3746774.png

    # curl -x127.0.0.1:80 bbb.com  //目前是没有这个网站的,因此我们访问到的是默认的虚拟主机

181041_QBNH_3746774.png

8、如何区分默认虚拟主机和非默认虚拟主机

    nginx找server的时候肯定会从第一个开始,因此在没有设定默认虚拟主机时,nginx会自动将排在第一个的server作为默认虚拟主机

    另外就是找带有default_server标记的server即为默认虚拟主机

三 Nginx用户认证

1、创建一个测试用的虚拟主机

    首先切换到vhost虚拟主机配置文件目录下,新增一个虚拟主机配置文件

    # test.com.conf 

182248_M53h_3746774.png

    在这个文件里写入以下内容:

server

{

    listen 80;

    server_name test.com;

    index index.html index.htm index.php;

    root /data/wwwroot/test.com;

   

location  /

    {

        auth_basic              "Auth";   //用于定义用户认证的名字

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;   //用户名密码文件

    }

}

2、生成密码文件

    使用apache2.4自带的htpsswd命令或文件来生成密码文件,如果没有安装apache,可以使用yum命令安装一个

    # /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lijie
其中-c指创建,/usr/local/nginx/conf/htpasswd是存放路径 ,lijie是用户名

183141_9zBc_3746774.png

按照上图提示输入两次密码后,我们再来查看密码文件内容

183248_on3t_3746774.png

接下来再来创建一个用户就不用加-c选项了

183504_60Xx_3746774.png

3、测试语法并重新加载

    # /usr/local/nginx/sbin/nginx -t
    # /usr/local/nginx/sbin/nginx -s reload
4、访问这个测试虚拟主机

    # curl -x127.0.0.1:80 test.com -I    //提示401错误,需要用户认证

184001_jgli_3746774.png

    接下来我们输入用户名和密码来访问

    # curl -ulj:112233 -x127.0.0.1:80 test.com   //其中lj是用户名,112233是密码

184429_9Lmh_3746774.png

    我们看到上图提示404错误,这是因为我们之前没有创建访问的目录及索引页,现在我们来创建一个

184734_jTmB_3746774.png

    这时我们再来访问就可以看到index.html中的内容了

184751_LCsu_3746774.png

5、针对目录的用户认证

    我们只需要在虚拟主机配置文件/usr/local/nginx/conf/vhost/test.com.conf中作出修改即可

修改前的配置文件内容

185050_fXgZ_3746774.png

修改后的配置文件内容如下,表示是针对admin这个目录进行用户认证

185144_uko7_3746774.png

    修改后,访问test.com就不需要做用户认证,只有访问test.com/admin时才需要做用户认证

6、针对某个页面的用户认证

    访问某个页面才需要用户认证的话,需要将配置文件/usr/local/nginx/conf/vhost/test.com.conf修改为如下内容:

185626_WaVA_3746774.png

    上图中~表示匹配,~ admin.php表示匹配到admin.php这个页面就需要做用户认证

四 Nginx域名重定向

1、修改配置文件

    当前我们已经有test.com这个域名来访问test.com这个网站,为了使另外两个域名test2.com和test3.com也跳转到test.com这个网站,我们需要修改配置文件

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

    以下内容为域名重定向使用的代码

    if ($host != 'test.com' ) {

        rewrite  ^/(.*)$  http://test.com/$1  permanent;

    }

    修改前

200525_qLFp_3746774.png

    修改后,下图框中部分为新增内容:

200714_XSPt_3746774.png

    上面^/(.*)$是http://$host/(.*)$的简写,^表示的就是域名,(.*)$代表的是域名后面跟着的一长串,$1代表的就是(.*)

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

2、检查语法并重新加载

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

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

3、测试验证域名重定向

    验证访问test2.com,可以看到状态码301重定向到test.com

202525_iXNR_3746774.png

    验证访问test2.com/wrwer/err,可以看到状态码301重定向到test.com/wrwer/err

202719_w2WA_3746774.png

     验证访问test3.com/wrwer/err,可以看到状态码301重定向到test.com/wrwer/err

203326_3gRL_3746774.png

    验证访问test4.com/wrwer/err,提示页面未找到,实际上是跳转到了默认虚拟主机aaa.com去了。

203555_9sBh_3746774.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/u/3746774/blog/1632648

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278324.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

edge无法上网dns_如何在Microsoft Edge中通过HTTPS启用DNS

edge无法上网dnsMicrosoft will one day enable DNS over HTTPS (DoH) for all Windows applications, but you can enable it in the new version of Microsoft Edge today with a hidden flag. DoH will improve your security and privacy online, but it isn’t yet enable…

UIButton小结

前言 本来没有打算写这篇文章的, 主要是因为在工作中遇到一些同事再用 有UIButton的时候, 有些很基本的,系统API提供的都不知道, 例如 如何让UIButton的文字居上,居左, 居右, 居下对其等一些基本点, 为此我特地写了一下UIButton小结 UIButton回顾 继承关系 NSObject -> UIRe…

Channel Allocation HDU1373

染色问题&#xff1a;相邻不能染同一种颜色 最少需要的颜色的数量最大团点的数量 #include<bits/stdc.h> using namespace std;#define N 27int n; int mp[N][N]; int ans; int alt[N][N]; int Max[N];bool dfs(int cur,int tot)//cur是s1集合的个数 {if(0cur){if(tot>…

satis原理浅析

什么是satis 我们一般是从packagist获取composer包的&#xff0c;但这些都是公开的。那如果我们想创建自己的私有库呢&#xff0c;比如企业就会有这方便的需要&#xff0c;那我们就可以用satis来创建自己的私有库。 Satis 是一个静态的 composer 资源库生成器。它像是一个超轻量…

HDU - 5686-Problem B (递推+高精)

度熊面前有一个全是由1构成的字符串&#xff0c;被称为全1序列。你可以合并任意相邻的两个1&#xff0c;从而形成一个新的序列。对于给定的一个全1序列&#xff0c;请计算根据以上方法&#xff0c;可以构成多少种不同的序列。 Input 这里包括多组测试数据&#xff0c;每组测试数…

c#写字板实现加粗功能_Windows 7中写字板和绘画中的新功能

c#写字板实现加粗功能WordPad and Paint are often overlooked accessories included in all versions of Windows since 95. They are still included in Windows 7 and now have a new look with some enhanced features. Here we will take a look at some of the new impro…

浏览器加载静态资源文件异常解决办法

2019独角兽企业重金招聘Python工程师标准>>> 1 使用chrome浏览器加载静态资源文件(css、js等)异常导致cssh和js文件不生效&#xff0c;具体报错如下: Resource interpreted as Stylesheet but transferred with MIME type text/html 原因应该是网页文档类型不一致导…

POJChallengeRound2 Guideposts 【单位根反演】【快速幂】

题目分析&#xff1a; 这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$ 这个形式很像单位根反演。 单位根反演一般用于求&#xff1a;$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}f(x)^i $ 推理过程略&#xff0c;实际上也就是交换求和符号的事情。 接着就变…

用Emesene替换Windows Live Messenger

Tired of Windows Live Messenger bloat and wishing that there was a simpler and cleaner replacement that would let you use your live.com and hotmail.com accounts? Look no further, now you can have all that messenger goodness with Emesene! 厌倦了Windows Liv…

python爬虫笔记(七):实战(三)股票数据定向爬虫

目标分析及描述 #CrawBaiduStocksA.py import requests from bs4 import BeautifulSoup import traceback import redef getHTMLText(url):try:r requests.get(url)r.raise_for_status()r.encoding r.apparent_encodingreturn r.textexcept:return ""def getStockL…

myeclipse和maven的clean和build

转&#xff1a; 详解myeclipse和maven的clean和build 2018年04月20日 11:33:34 群星坠 阅读数&#xff1a;3529 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_35603331/article/details/80002723MyEclipse是一个被广为…

三星Galaxy S20:如何开启黑暗模式

Justin Duino贾斯汀杜伊诺(Justin Duino)Samsung was one of the first Android manufacturers to add Dark Mode to its handsets. If you recently purchased a Galaxy S20, S20, or S20 Ultra, enabling the UI feature and setting it up on a schedule is extremely easy.…

nginx和apache限制IP地址访问的设置方法

一、nginx禁止IP地址访问1、在nginx配置文件中加入这个&#xff1a;2、重启nginx服务二、apache禁止IP地址访问1、更改vhosts.conf文件&#xff1a;NameVirtualHost 192.168.1.191 <VirtualHost 192.168.1.191:99>#DocumentRoot "/usr/local/kk-mail/data/www"…

wordweb在线编辑_使用WordWeb享受按需词典和词库功能

wordweb在线编辑Run across an unusual word or need a synonym for a word quickly? Usually that means opening a browser and doing the appropriate search. Now you can have all that word power goodness at your fingertips with WordWeb. 遇到一个不寻常的词还是需…

转://RMAN跨平台可传输表空间和数据库

参考链接&#xff1a; http://blog.itpub.net/23135684/viewspace-776048/ http://blog.sina.com.cn/s/blog_69e7b8d7010164xh.html https://www.2cto.com/database/201311/260446.html 这篇文章翻译自Oracle 11gR2官方文档。详细讨论了使用RMAN工具的CONVERT DATAFILE&#xf…

2139=数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

1 #include<stdio.h>2 #include<string.h>3 int map[1000][1000],visit[1000];4 int step,mark;5 int queue[1000];//用来储存已经遍历了的数据。6 void BFS(int k)7 {8 int i,o0,p0,temp,end0;//temp用来表示当前所在地。o表示下一步从哪个顶点向下出发。9 …

vnc数量限制_通过限制视觉效果在Vista上加速VNC

vnc数量限制This article was written by MetrotekGeek from Metrotek Solutions, a friend of the How-To Geek 本文由Metrotek Solutions的MetrotekGeek撰写&#xff0c;Metrotek Solutions是How-To Geek的朋友 As a computer field tech, I use the remote desktop program…

思科AP-什么是COS AP?

COS:Click OS 所有新的wave 2 AP都带有COS。它建立在IOS之上&#xff0c;但behaves 不同。 COS APs是Click OS APs&#xff08;较新的AP型号&#xff0c;Wave 2等&#xff09; 例如&#xff1a;18xx&#xff0c;28xx&#xff0c;38xx&#xff0c;48xx型号Click OS APs或COS AP。…

[转帖]外壳命名空间扩展

一般介绍 很多人一定用过ZipMagic&#xff0c;对它能把一个压缩文件映射成文件夹感到很奇怪&#xff0c;不知道它使用了什么技术&#xff0c;实际上它用到的技术就是实现了一个外壳的命名空间扩展&#xff08;Shell Namespace Extention&#xff09;。 文件夹和视图&#xff1a…

使Safari在Windows Vista上每20秒停止崩溃

The new Safari for Windows is a very slick browser that beats the pants off everything else in the speed department, but it crashes so much on Windows Vista that it’s virtually unusable. 新的Windows版Safari浏览器非常流畅&#xff0c;可以超越速度部门的所有…