九月 19, 2014 | 後端和Drupal

Drupal 7 + Clean URL 在 Ubuntu 12.04 + Nginx 安裝流程

前言 Nginx 已經成為了現在很多大型網站的另外一個解答,不僅輕巧,而且效能、速度、穩定度都更好已經有不少公司已經從Apache轉到了Nginx的懷抱之中,我們也不例外。以下將使用Ubuntu12.04搭配者Nginx Server +MySQL +  Drush ,並且安裝完成Drupal 7。 Step 1:更新系統 先做所有套件系統的更新 $ sudo apt-get update $ sudo apt-get upgrade  Step2: 安裝MySQL $ sudo apt-get install mysql-server  Step3:安裝Nginx $ sudo apt-get install nginx $ sudo service nginx start若是都成功啟用了,這個時候進入這個IP的網頁,就可以順利看到下圖  

前言

Nginx 已經成為了現在很多大型網站的另外一個解答,不僅輕巧,而且效能、速度、穩定度都更好已經有不少公司已經從Apache轉到了Nginx的懷抱之中,我們也不例外。以下將使用Ubuntu12.04搭配者Nginx Server +MySQL +  Drush ,並且安裝完成Drupal 7。

Step 1:更新系統

先做所有套件系統的更新

$ sudo apt-get update
$ sudo apt-get upgrade 

Step2: 安裝MySQL

$ sudo apt-get install mysql-server 

Step3:安裝Nginx

$ sudo apt-get install nginx

$ sudo service nginx start若是都成功啟用了,這個時候進入這個IP的網頁,就可以順利看到下圖

 

Step4:安裝 PHP-FPM

sudo apt-get install php5-fpm

Step5:設定PHP-FPM

編輯”/etc/php5/fpm/php.ini“這個檔案,並且將cgi.fix_pathinfo值更改為0還有拿掉前面的「;」符號,也就是讓這行程式執行的意思。

Step6:設定/etc/php5/fpm/pool.d/www.conf

將裏面的一行字 listen = 127.0.0.1:9000 改成 listen = /var/run/php5-fpm.sock

設定完成之後,再將下面的三行 listen.owner = www-data listen.group = www-data listen.mode = 0660 前面的「;」拿掉,儲存。

重新啟動php5-fpm

sudo service php5-fpm restart  其實,到這裡已經算是完成了php在Nginx上面的安裝了,可以利用以下進行測試 分成以下三個步驟: 進入/etc/nginx/sites-available/ 編輯default檔案 加入index.php 還有 uncomment 下面的php設定

在index的後面加入index.php,並且再往下找到location ~ .php$的地方,uncomment php的設定 在這裡請務必注意,不要uncomment  "fastcgi_pass 127.0.0.1:9000;",因為底下已經有了 fastcgi_pass unix:/var/run/php5-fpm.sock;

再來進入/usr/share/nginx/www/ 修改我們的index.html吧 首先我們將index.html改成index.php,然後裏面加入了簡單印出phpinfo的語法

再重新回到網頁就可以得到以下結果

看到這個畫面,代表您已經成功的在Ubuntu上面安裝了Nginx+php的服務,並且也可以看到FPM/FASTCGI 接下來我們就繼續往下走!  

Step7:安裝php5-mysql 還有 php5-gd

當然目標是安裝Drupal,接著將其他相關需要的套件安裝上去

$ sudo apt-get install php5-mysql
$ sudo apt-get install php5-gd安裝完這些套件,請務必再重新啟動php5-fpm這個服務

Step8:安裝Drush

Drush是Drupal不可或缺的一個原件,有了Drush可以讓我們更方便控制Drupal。

sudo apt-get install php-pear
pear channel-discover pear.drush.org
pear install drush/drush
drush version

Step9:安裝Drupal

將Drupal的元件下載到/usr/share/nginx/www 裏面 重新進入網站首頁,應該就可以看到了Drupal的安裝畫面了

$ drush dl drupal
$ mv drupal-7.31 www
$ mysql -p
$ create database example;
$ create user test@localhost identified by 'test123';
$ grant all privileges on example.* to test@localhost;
完成資料庫安裝以及Drupal安裝。

Step10:設定Clean URL

安裝好Drupal之後,再來一個重點就是Clean URL的部分了 做法很簡單,就是去設定「網站設定檔」,編輯 /etc/nginx/sites-available/default 更改“location /” 以及加入 “location @drupal”

location / {
                 root   /usr/share/nginx/www/;
                 index  index.php;
                 error_page 404 = @drupal;
        }
location @drupal {
            rewrite ^(.*)$ /index.php?q=$1 last;
 }

如下圖

更改完畢,重新啟動nginx,完成。 2015/07/08 原本的Drupal.org那篇Clean URLs with NGINX 說已經不需要特別設定了可是需要到nginxwiki更改一下設定 可以試試看

location / {
                # This is cool because no php is touched for static content
                try_files $uri @rewrite;
        }
 location @rewrite {
                # Drupal in a subdirectory
                rewrite ^/([^/]*)/(.*)(/?)$ /$1/index.php?q=$2&$args;
        }

就可以成功開啟clean URLs

 

Step11:設定PHP執行時間+上傳檔案大小+記憶體大小

  1. 設定php執行時間 針對個別的網站進行設定,例如example.com

編輯 /etc/nginx/sites-available/example.com location ~ .php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_read_timeout 300; }如果是針對全部網站進行設定

編輯 /etc/nginx/nginx.conf http { #... fastcgi_read_timeout 300;  #... }2.設定上傳檔案大小 編輯 /etc/php5/fpm/php.ini

upload_max_filesize = 100M post_max_size = 100M再編輯/etc/nginx/nginx.conf

http { #... client_max_body_size 100m; #... }3.設定記憶體大小 編輯 /etc/php5/fpm/php.ini memory_limit=512M 看您的選擇,要多少記憶體限制了~   到這裡算是全部設定完成!!!!!      

參考資料:

  • How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04
  • Running Drupal on Nginx with Memcached
  • nginx error connect to php5-fpm.sock failed (13: Permission denied)
  • Increase PHP script execution time with Nginx
  • How To Migrate from an Apache Web Server to Nginx on an Ubuntu VPS
  • Linux – How to configure NGINX + PHP-FPM + Drupal 7.0
  • Clean URLs with NGINX
  • Increase PHP script execution time with Nginx
  • Nginx error 413: Request entity too large Quick Fix
  • Drupal-Nginx Community
hashtags:

使用我們的服務即表示您同意Cookie政策。了解更多