当前位置:首页 > linux教程 > 列表

Linux系统下安装配置Nginx环境整理

发布:smiling 来源: PHP粉丝网  添加日期:2015-05-05 15:47:00 浏览: 评论:0 

安装Nginx是做WEB服务器的一个选项之一了,如果网站静态多我们多半使用nginx否则会选择apache了,下面我们不说远了就来看看Linux系统下安装配置Nginx环境整理吧.

PHP的这些环境在linux下也折腾过很多次了,每次重装都要重新去找这些文档,记性不好,还是自己整理下吧.

Nginx安装:

  1. # yum -y install gcc* pcre glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel lrzsz 
  2.  
  3. # groupadd www && useradd www -g www 
  4.  
  5. # wget http://nginx.org/download/nginx-1.6.1.tar.gz 
  6.  
  7. # tar zxvf nginx-1.6.1.tar.gz 
  8.  
  9. # cd nginx-1.6.1 
  10.  
  11. # ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --error-log-path=/data/logs/nginx/error.log --http-log-path=/data/logs/nginx/access.log 
  12.  
  13. # make && make install 

机器为阿里云512MCentOS,刚初始化的机器发现没有make命令,通过yum安装即可.

# yum -y install make

常用操作:

  1. -- root软连接到nginx.conf 
  2. # ln -s /usr/local/webserver/nginx/conf/nginx.conf /root/nginx.conf 
  3.    
  4. -- root目录下直接重启脚本 
  5. # echo -e  '#!/bin/bash \n /usr/local/webserver/nginx/sbin/nginx -s reload ' >> /root/nginx_restart.sh 
  6.    
  7. -- 添加执行权限 
  8. # chmod +x /root/nginx_restart.sh 
  9.    
  10. -- 添加到自启动 
  11. # echo '/usr/local/webserver/nginx/sbin/nginx' >>/etc/rc.local 

启动Nginx:

  1. # /usr/local/webserver/nginx/sbin/nginx 
  2.    
  3. -- 检测是否配置文件是否正确 
  4. # /usr/local/webserver/nginx/sbin/nginx -t 
  5.    
  6. -- 重启nginx 
  7. # /usr/local/webserver/nginx/sbin/nginx -s reload 
  8. nginx.conf 

配置示例,通过vhost来配置新站点,避免nginx.conf文件过长,不方便管理.

  1. # more /root/nginx.conf  
  2.  
  3. user  www www; 
  4.  
  5. worker_processes  1; 
  6.  
  7.    
  8.  
  9. error_log  logs/error.log; 
  10.  
  11. pid        logs/nginx.pid; 
  12.  
  13.    
  14.  
  15. events { 
  16.  
  17.     use epoll; 
  18.  
  19.     worker_connections  1024; 
  20.  
  21. } 
  22.  
  23.    
  24.  
  25. http { 
  26.  
  27.     <a href="/tags.php/include/" target="_blank">include</a>       mime.types; 
  28.  
  29.     default_type  application/octet-stream; 
  30.  
  31.    
  32.  
  33.     server_names_hash_bucket_size 128; 
  34.  
  35.     client_header_buffer_size 32k; 
  36.  
  37.     large_client_header_buffers 4 32k; 
  38.  
  39.     client_max_body_size 50m; 
  40.  
  41.    
  42.  
  43.     log_format  main  '$remote_addr - $remote_user [$time_local] "$<a href="/tags.php/request/" target="_blank">request</a>" ' 
  44.  
  45.                       '$status $body_bytes_sent "$http_referer" ' 
  46.  
  47.                       '"$http_user_agent" "$http_x_forwarded_for"'; 
  48.  
  49.     sendfile        on; 
  50.  
  51.     tcp_nopush     on; 
  52.  
  53.    
  54.  
  55.     keepalive_timeout  60; 
  56.  
  57.     tcp_nodelay on; 
  58.  
  59.    
  60.  
  61.     fastcgi_connect_timeout 300; 
  62.  
  63.     fastcgi_send_timeout 300; 
  64.  
  65.     fastcgi_read_timeout 300; 
  66.  
  67.     fastcgi_buffer_size 64k; 
  68.  
  69.     fastcgi_buffers 4 64k; 
  70.  
  71.     fastcgi_busy_buffers_size 128k; 
  72.  
  73.     fastcgi_temp_file_write_size 128k; 
  74.  
  75.    
  76.  
  77.     gzip  on; 
  78.  
  79.     gzip_min_length  1k; 
  80.  
  81.     gzip_buffers     4 16k; 
  82.  
  83.     gzip_http_version 1.0; 
  84.  
  85.     gzip_comp_level 2; 
  86.  
  87.     gzip_types       text/plain application/x-<a href="/js_a/js.html" target="_blank">javascript</a> text/css application/xml; 
  88.  
  89.     gzip_vary on; 
  90.  
  91.    
  92.  
  93.     include vhost/*.conf; 
  94.  
  95. } 

添加80端口:

  1. server { 
  2.  
  3.     listen 80; 
  4.  
  5.     server_name localhost; 
  6.  
  7.     index index.htm index.html index.php; 
  8.      --phpfensi.com 
  9.    
  10.  
  11.     root /data/www; 
  12.  
  13.     access_log /data/logs/nginx/default.access.log; 
  14.  
  15. }

Tags: 配置Nginx Linux环境

分享到: