1. 程式人生 > >Centos下安裝nginx步驟解析

Centos下安裝nginx步驟解析

The 建立 簡單 end timeout 連接 obj ror kill

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

1、編譯環境gcc g++ 開發庫之類的需要提前裝好

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

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

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

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

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

  編譯:./configure

  安裝:make && make install

  查看安裝版本:pcre-config --version 如果出現版本號,說明安裝成功

  

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

  rpm -qa pcre 如果沒有顯示說明沒有安裝 反之安裝過

  rpm -e --nodeps pcre 刪除pcre

3、安裝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

解決辦法:

1、首先確認已經安裝好pcre 軟件(nginx 依賴該軟件)

2、創建軟連接

對於/lib/* 32位系統來說:

1 #查看lib庫
2 # ls /lib/ |grep pcre
3 libpcre.so.0
4 libpcre.so.0.0.1
5 #添加軟連接
6 # ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1

ps: 也有可能 pcre lib文件在目錄:/usr/local/lib/

對於/lib64/* 64位系統來說:

1 #查看lib庫
2 # ls /lib64/ |grep pcre
3 libpcre.so.0
4 libpcre.so.0.0.1
5 #添加軟連接
6 # ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1

ps: 也有可能 pcre lib文件在目錄:/usr/local/lib64/。

  (如果提示目錄不存在,則需要做一下處理:簡單的作法:

mkdir -p /var/www
然後再運行ln -s 命令。

復雜點需要確認幾點:
先確認/usr/share/phpmyadmin/這個目錄存在。
再確認/var/www/這個目錄是否存在。
然後要確定下你是想把/usr/share/phpmyadmin/ 這個目錄鏈接到/var/www/phpmyadmin,還是/var/www
如果是前者,需要先確保/var/www這個目錄存在,然後再運行你說的ln命令。
如果是後者,必須要確保沒有/var/www這個目錄,然後命令最後的/去掉,變成這樣:
sudo ln -s /usr/share/phpmyadmin/ /var/www <- 註意,最後沒有‘/‘

  )

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

4、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

  技術分享圖片

  說明配置成功

5、啟動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

  技術分享圖片

  至此,安裝配置完成!

  在學習過程中配到問題:

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

  2、菜鳥網站中編譯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位的配置文件路徑有關系,有待驗證)  

6、卸載nginx

  刪除nginx文件即可

  rm -rf /usr/local/nginx

  

Centos下安裝nginx步驟解析