您的位置:首页 > 运维架构 > Nginx

nginx安装配置

2016-01-07 16:32 656 查看




1、为什么选择nginx

Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.

作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。

Nginx 安装非常的简单,配置文件 非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。


2、安装nginx

ngnix依赖以下模块

gzip模块需要zlib库

rewrit模块需要pcre库

ssl功能需要openssl库


1、安装pcre

获取pcre编译安装包,在可以在 http://www.pcre.org/ 点击download获取,地址为 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 我下载的prce-8.37.tar.gz

解压tar包 ,我这里放入到根目录下的soft目录里

lin@ubuntu:/soft$ls

jdk1.7.0_79jdk-7u79-linux-x64.tar.gzpcre-8.37.tar.gz

lin@ubuntu:/soft$tar -zxvf pcre-8.37.tar.gz

[/code]

进入解压缩目录,执行./configure。

lin@ubuntu:/soft/pcre-8.37$ ./configure

[/code]

make & make install

lin@ubuntu:/soft/pcre-8.37$ make

lin@ubuntu:/soft/pcre-8.37$ sudo make install

[/code]

2、安装openssl

获取openssl编译安装包,在http://www.openssl.org/source/上可以获取当前最新的版本。我使用的是openssl-1.0.0t.tar.gz版本

解压tar包 ,我这里放入到根目录下的soft目录里

lin@ubuntu:/soft$tar -zxvf openssl-1.0.0t.tar.gz

[/code]

进入解压缩目录,执行./config。

lin@ubuntu:/soft/openssl-1.0.0t$ ./config

[/code]

make & make install

lin@ubuntu:/soft/openssl-1.0.0t$ make

lin@ubuntu:/soft/openssl-1.0.0t$ sudo make install

[/code]


3、安装zlib

获取zlib编译安装包,在http://www.zlib.net/上可以获取当前最新的版本。我使用的是zlib-1.2.8.tar.gz版本

解压缩tar.gz包 我的是放入根目录soft里面。

lin@ubuntu:/soft$
tar -xf zlib-1.2.8.tar.gz

[/code]

进入解压缩目录,执行./configure。

lin@ubuntu:/soft/zlib-1.2.8$ ./configure

[/code]

make & make install

lin@ubuntu:/soft/
zlib
-
1.2.8
$ make

lin@ubuntu:/soft/
zlib
-
1.2.8
$ sudo make install

[/code]


4、安装nginx

获取nginx,在http://nginx.org/en/download.html上可以获取当前最新的版本。我使用的是nginx-1.8.0.tar.gz版本

解压缩tar.gz包,我的放入到根目录下的soft里面。

lin@ubuntu:/soft$tar -zxvf nginx-1.8.0.tar.gz 

[/code]

进入解压缩目录,执行./configure

lin@ubuntu:/soft/nginx-1.8.0$ ./configure

[/code]

make & make install

lin@ubuntu:/soft/nginx-1.8.0$ make

lin@ubuntu:/soft/nginx-1.8.0$ sudo make install

[/code]

到此安装完毕,环境准备结束


3、简单启动nginx

由于nginx占用的是80端口,所以先确保系统的80端口没有被其他程序占用

1、启动nginx

lin@ubuntu:/usr/local/nginx$ sudo sbin/nginx

[/code]

2、检查是否启动成功
netstat -ano|grep 80

[/code]

有输出就证明启动成功

3、访问80端口,打开浏览器输入nginx所在的电脑ip出现 Welcome to nginx! 则证明成功!

像重启的话 可以用
lin@ubuntu:/usr/local/nginx$ sudo sbin/nginx -s reload

[/code]

4、停止服务可以用

lin@ubuntu:/usr/local/nginx$ sudo killall nginx

[/code]


4、nginx配置

安装完成后,配置目录conf下有很多配置文件,其中除了nginx.conf,其余的配置文件,使用默认提供的即可。

nginx.conf是主配置文件其中

work_process 表示工作进程的数量,一般设置为cpu的核数。

worker_connections 表示每个工作进程的最大连接数

server{}定义了虚拟机主机

listen 监听端口;

server_name 监听域名

location/{} 是用来为匹配的 URI 进行配置,URI 即语法中的“/uri/”。

location/ { }匹配任何查询,因为所有请求都以 / 开头。

root指定对应uri的资源查找路径,这里html为相对路径,完整路径为/opt/ opt/nginx-1.7.7/html/

index指定首页index文件的名称,可以配置多个,以空格分开。如有多个,按配置顺序查找。

下面是默认配置
#usernobody;

worker_processes1;


#error_loglogs/error.log;

#error_loglogs/error.lognotice;

#error_loglogs/error.loginfo;


#pidlogs/nginx.pid;



events {

worker_connections1024;

}



http {

include mime.types;

default_typeapplication/octet-stream;


#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '

#'$status $body_bytes_sent "$http_referer" '

#'"$http_user_agent" "$http_x_forwarded_for"';


#access_loglogs/access.logmain;


sendfileon;

#tcp_nopush on;


#keepalive_timeout0;

keepalive_timeout65;


#gzipon;


server {

listen 80;

server_namelocalhost;


#charset koi8-r;


#access_loglogs/host.access.logmain;


location / {

root html;

indexindex.html index.htm;

}


#error_page404/404.html;


# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504/50x.html;

location = /50x.html {

root html;

}


# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#proxy_pass http://127.0.0.1;[/code] 
#}


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

#root html;

#fastcgi_pass 127.0.0.1:9000;

#fastcgi_indexindex.php;

#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

#includefastcgi_params;

#}


# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#denyall;

#}

}



# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#listen 8000;

#listen somename:8080;

#server_namesomenamealiasanother.alias;


#location / {

#root html;

#indexindex.html index.htm;

#}

#}



# HTTPS server

#

#server {

#listen 443 ssl;

#server_namelocalhost;


#ssl_certificatecert.pem;

#ssl_certificate_keycert.key;


#ssl_session_cacheshared:SSL:1m;

#ssl_session_timeout5m;


#ssl_ciphersHIGH:!aNULL:!MD5;

#ssl_prefer_server_cipherson;


#location / {

#root html;

#indexindex.html index.htm;

#}

#}


}

[/code]

很容易看出来 nginx监听了80端口,域名为loccalhost,根路径为html文件夹,默认文件为index.html,index.htm,服务器错误重定向到50x.html

可以看到nginx html文件夹下面有如下文件
lin@ubuntu:/usr/local/nginx$ cd html

lin@ubuntu:/usr/local/nginx/html$ ls

50x.htmlindex.html

[/code]

这就是刚刚上面所说的启动nginx时候访问的页面,其实就是这里的index.html页面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: