Nginx安装与配置
Nginx的安装过程比较简单,但是它的配置相对复杂点。特别是刚接触和使用nginx的时候,不明白里面的参数的含义,这时候需要去阅读nginx的相关文档。下面的安装过程和配置参数均在生产服务器上实验过多次。
1、安装nginx
Nginx下载地址:http://nginx.org/en/download.html
先要安装pcre、openssl、zib等模块
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ./configure --user=daemon --group=daemon --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make -j 8 make install
2、修改conf/nginx.conf里面的默认参数
# nginx.conf
user daemon daemon;
worker_processes 8;
worker_rlimit_nofile 65536;
pid /usr/local/nginx/nginx.pid;
error_log /usr/local/nginx/logs/nginx_error.log info;
events
{
use epoll;
worker_connections 65536;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 320k;
large_client_header_buffers 4 320k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 6000s;
tcp_nodelay on;
fastcgi_connect_timeout 600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml application/json audio/aac;
gzip_vary on;
# Load config files from the directory
include /usr/local/nginx/sites-enabled/*.conf;
}上面的配置是nginx的全局配置,如nginx的总进程数、打开的文件数据、I/O方式采用epoll,gzip压缩参数。
注意上面的最后一行:include /usr/local/nginx/sites-enabled/*.conf;
采用了类似与Apache的目录结构,这样可以针对不同的Web应用设置不同的参数,也方便管理。
3、设置具Web应用参数
在sites-enabled里面设置针对每个Web应用的配置参数,主要是基本的虚拟主机配置(server),包括Web应用根目录,侦听端口,ServerName,CGI,日志等。
下面是一个常见的虚拟主机vhost的配置,其中包括php-fpm CGI的调用配置参数。
# mdba.cn.conf
server
{
listen 80;
server_name mdba.cn www.mdba.cn 112.124.65.179;
root /usr/local/louvre-wp;
index index.php;
location ~ "\.(js|ico|gif|jpg|png|css)$" {
expires 1w;
}
location / {
fastcgi_ignore_client_abort on;
#fastcgi_pass unix:/dev/shm/php.socket;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
index index.php;
if (!-e $request_filename) {
rewrite . /index.php last;
break;
}
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
log_format louvre '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$request" "$http_referer" '
'"$http_user_agent" "$request_time" ';
access_log /usr/local/nginx/logs/access_louvre.log louvre;
}fastcgi_pass 127.0.0.1:9000;设定的端口是9000,设定之前检查该端口是否已经被其他服务器进程占用了。另外http侦听端口可以不使用默认的80端口。
4、启动nginx与测试
#-t选项可以测试配置脚本是否有语法错误,如缺少”;”、变量未定义或者拼写有误之类的常见语法问题。修改nginx配置文件,需要重启nginx才能使新的配置生效。
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx
测试nginx与调用php-fpm是否正常工作,在上面的设定的root目录下面,新建index.php文件。
<?php phpinfo(); ?>
浏览器里面输入112.124.65.179(这是我的阿里云主机的IP,或者自己的本地IP),就以看到php的一些运行环境
下面关于Nginx的文章您也可能喜欢,不妨参考下:
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里