阿里云ECS每天一件事D6:安装nginx-1.6.2

时间:2023-03-09 09:09:21
阿里云ECS每天一件事D6:安装nginx-1.6.2

自从接触nginx就开始喜欢上这个小东西了,似乎没什么特别的原因,就是喜欢而已。

1、安装环境的准备

yum install pcre pcre-devel openssl openssl-devel

由于前面的安装,大多数环境和类库已经准备完毕,只需要安装rewrite依赖和ssl相关的组件即可。

2、编译配置

./configure --prefix=/usr/local/nginx --user=www-data --group=www-data\
--with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module\
--with-mail

常规性编译配置,大多数标准组件,nginx编译时会默认编译

3、安装

make && make install

4、启动操作

 /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx –s stop|reload

nginx的操作基本都是通过nginx这个命令进行的,启动可直接运行1,停止,或者修改完配置文件重新加载,可以运行带有-s参数的命令执行

5、配置

nginx的配置文件位于/usr/local/nginx/conf/nginx.conf。

打开后,注意如下的基础配置语句:

 user www-data www-data;
worker_processes ; events {
worker_connections ;
} http {
include mime.types;
include vhost/*.conf;
default_type application/octet-stream;
index index.php index.html;
}

1)运行nginx的用户和组

2)nginx可使用的cpu内核数,默认是1,worker_processes*worker_connections=实际可接受的用户链接数字;

5)设置可接受的连接数;

10)自定义配置,标示关于虚拟主机的配置文件,在conf目录的vhost子目录中(注意此为自定义配置,只有在http全局设置中使用了include加载全部虚拟主机配置方可有效)。

虚拟主机配置:

 server {
listen ;
server_name example.org www.example.org;
root /data/www;
access_log logs/example/access.log; location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
} location ~* \.(gif|jpg|png)$ {
access_log off;
expires 5d;
} location ~ /\.ht {
deny all;
}
}

具体配置内容,可参考wiki.nginx.org

6、默认主机的设置

配置完上面的虚拟主机,有时你会发现一个有意思的事情,如果你绑定一个未定义的主机头,依旧是可以访问的,只不过访问的是,第一个或者最后一个虚拟主机,这是因为nginx默认没有找到主机头时,会指定一个,因此最好在主配置文件中,设置默认的server配置节,已避免非授权的绑定。

server {
listen default_server;
server_name localhost;
location / {
root html;
}
}

注意,上面的关键词为default_server,设置一个空的虚拟主机,将其监听设置为default_server,这样一来,所有为在虚拟主机中设定的主机头,及时绑定之后,也会默认跳转到这个空主机中。