2016年5月28日 星期六

在 CentOS7/RHEL7 上架設 Nginx Web Server(一)

設定目標:
  • 在 Centos 7 上架設 Nginx Web Server!
Nginx Web Server 快速設定流程:
  1. 編寫最新的 Nginx 套件位置:
    #vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
    
  2. 利用 yum 安裝
    #yum -y install nginx
    
  3. 啟動防火牆:
    #firewall-cmd --permanent --zone=public --add-service=http
    #firewall-cmd --permanent --zone=public --add-service=https
    #firewall-cmd --reload
    
  4. 啟動 Nginx:
    #systemctl enable nginx.service
    #systemctl start nginx
    

讓 Nginx 加入 php 模組:
  1. 利用 yum 安裝
    #yum -y install php-fpm
    
  2. 編寫 php-fpm 預設檔:
    #vim /etc/php-fpm.d/www.conf
    user = nginx
    group = nginx
    
  3. 啟動 php-ftm 功能:
    #systemctl enable php-fpm.service
    #systemctl start php-fpm
    
  4. 編寫 Nginx 預設檔:
    #vim /etc/nginx/conf.d/default.conf
    (修改下列項目:)
     location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass unix:/var/run/php-fpm/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
     location ~ /\.ht {
            deny  all;
        }
    
  5. 重新啟動 Nginx:
    #systemctl restart nginx
    
  6. 改成 Socket 的使用方式:
    #vim /etc/sysconfig/memcached
    OPTIONS="-s /tmp/memcached.sock -a 666"
    #MEMCACHED_ARGS="-s /tmp/memcached.sock -a 666"
    
  7. 檢查一下成果:
    #echo stats | nc -U /tmp/memcached.sock
    
  8. 修改 memcached.ini 設定檔內容:
    #/etc/php/conf.d/memcached.ini
    extension=memcached.so
    session.save_handler="memcached"
    session.save_path="/tmp/memcached.sock"
    
  9. 編修 Nginx 預設檔:
    #vim /etc/nginx/conf.d/default.conf
    server {
       :
    (加入以下內容:)
       location ^~ /cache/ {
               set            $memcached_key $request_uri;
               #memcached_pass 127.0.0.1:11211;
               memcached_pass unix:/tmp/memcached.sock;
        }
        :
    
  10. 重新啟動 Nginx:
    #systemctl restart nginx
    

參考文獻:

  1. https://www.howtoforge.com/tutorial/install-nginx-with-php-and-mysql-lemp-stack-on-centos/
  2. http://blog.itist.tw/2016/01/installing-lemp-stack-with-centos-7-nginx-mariadb-php-7.html
  3. http://www.phpini.com/linux/rhel-centos-7-install-nginx-mysql-php-lemp
  4. http://www.server-world.info/en/note?os=CentOS_7&p=nginx&f=1
  5. https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-centos-7