CentOS安装Nginx

  |   0 评论   |   410 浏览

1.安装编译工具和库文件

[root@localhost /]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl 

2.安装pcre

让nginx支持Rewrite

2.1下载

[root@localhost /]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2.2 解压

[root@localhost /]# tar zxvf pcre-8.35.tar.gz

2.3编译安装

[root@localhost pcre-8.35]# ./configure
[root@localhost pcre-8.35]# make && make install

3.安装nginx

3.1下载

[root@localhost /]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

3.2解压

[root@localhost /]# tar zxvf nginx-1.6.2.tar.gz

3.3安装

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/pcre-8.35
[root@localhost nginx-1.6.2]# make
[root@localhost nginx-1.6.2]# make install

4.配置

配置Nginx运行使用的www用户

[root@localhost sbin]#  /usr/sbin/groupadd www
[root@localhost sbin]#  /usr/sbin/useradd -g www www

修改nginx.conff配置文件

user www www; #账户
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid; #进程pid存放位置
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535; #最大文件打开数(连接),
events
{
  use epoll;#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以提高nginx性能
  worker_connections 65535;#单个后台worker process进程的最大并发链接数
}
http
{
  include mime.types;#文件拓展名于类型映射表
  default_type application/octet-stream;#默认文件类型
  #日志模式
  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
  
#charset gb2312;
  #请求缓存
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;#开启高效传输模式
  tcp_nopush on;#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布
  keepalive_timeout 60;#http连接超时时间(单位:秒)
  tcp_nodelay on;#激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
  # FastCGI相关参数:为了改善网站性能:减少资源占用,提高访问速度
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  # 压缩相关配置
  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;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虚拟主机的配置
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名
    index index.html index.htm index.php;#首页排序
    root /usr/local/nginx/html;#站点目录
    #error_page 500 502 404 /templates/kumi/phpcms/404.html; #错误页面
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

检查配置文件修改是否正确

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

[root@localhost sbin]# /usr/local/nginx/sbin/nginx 
[root@localhost sbin]# ps -ef | grep nginx
root      11075      1  0 01:11 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
www       11076  11075  0 01:11 ?        00:00:00 nginx: worker process      
www       11077  11075  0 01:11 ?        00:00:00 nginx: worker process      
root      11079   1562  0 01:11 pts/0    00:00:00 grep nginx

其他命令

/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx

访问:

参考链接:http://www.runoob.com/linux/nginx-install-setup.html

这个家伙很懒,连签名都不写。

评论

发表评论