Centos下安裝nginx步驟解析

 最近研究LNMP,首先要在linux下配置nginx服務器,話很少說,上步驟javascript

一、編譯環境gcc g++ 開發庫之類的須要提早裝好php

  yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-develcss

二、首先安裝PCRE  pcre功能是讓nginx有rewrite功能html

  下載PCRE:wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gzjava

  解壓安裝包:tar zxvf pcre-8.35.tar.gznode

  進入安裝包目錄:cd pcre-8.35linux

  編譯:./configure
nginx

  安裝:make && make installweb

  查看安裝版本:pcre-config --version   若是出現版本號,說明安裝成功服務器

  

  檢查系統裏是否安裝了pcre軟件

  rpm -qa pcre   若是沒有顯示說明沒有安裝  反之安裝過

  rpm -e --nodeps pcre  刪除pcre

三、安裝nginx

  下載nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz

  解壓安裝包: tar zxvf nginx-1.6.2.tar.gz

  進入安裝包目錄:cd nginx-1.6.2

  編譯安裝:./configure  默認地址 /usr/local/nginx

  安裝:make

  安裝:make install  

  注:

  第一次編譯的時候:

  使用64位的系統第一次編譯安裝出現

  error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory這種狀況,nginx默認是在lib64下,32爲的是在lib下

  查找:find / -name 'libpcre.so.1'  出現 /usr/local/lib/libpcre.so.1,咱們創建以符號連接:ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1

  這樣能夠查看nginx版本 /usr/local/nginx/sbin/nginx -v  出現版本號

四、nginx配置

  cd /usr/local/nginx/conf ,把下面的內容覆蓋到nginx.conf,內容從菜鳥網站上搜索的

user www www;
worker_processes 2; #設置值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日誌位置和日誌級別
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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;
  keepalive_timeout 60;
  tcp_nodelay on;
  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;#站點目錄
      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;
  }

}

  能夠檢測配置的是否正確

  /usr/local/nginx/sbin/nginx -t

  

  說明配置成功

五、啓動nginx

  啓動的時候有報錯,以下

  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)...

  這說明80接口有被佔用,查看接口

  netstat -ntpl

  kill -9 $pid //殺掉進程

  再次啓動

  /usr/local/nginx/sbin/nginx

  中止服務器

  /usr/local/nginx/sbin/nginx -s stop 或 /usr/local/nginx/sbin/nginx -s quick

  網頁訪問  127.0.0.1

  

  至此,安裝配置完成!

  在學習過程當中配到問題:

  一、在下載安裝pcre和nginx中,要用root身份,通常下載到/usr/local/src/,下載到其餘地方配置呢?(測試幾回沒成功)

  二、菜鳥網站中編譯nginx的時候 配置安裝地址 

   ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35

   編譯不成功?(配置安裝路徑應該沒問題,編譯的時候報pcrelib.so.0不存在,安裝的時候webserver文件沒有創建成功,想着應該仍是和64位的配置文件路徑有關係,有待驗證)  

六、卸載nginx

  刪除nginx文件便可

  rm -rf /usr/local/nginx

相關文章
相關標籤/搜索