在Codeigniter 上開發了也快一年,前陣子被推坑來laravel,一試完果真回不去了,簡便的指令模式能快速地建立Controller、Model、View 及各種樣板,當下果斷放棄Codeigniter。
在這框架奔放的年代,Laravel 絕對是PHP後端框架最重要的角色沒有之一。
設定 Yum Repositories
1 2
| $ sudo yum install epel-release -y $ sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
|
webtatic 的倉庫是為了輔助安裝php7 以上的版本。
安裝套件
1
| $ sudo yum install php71w php71w-opcache php71w-fpm php71w-mbstring php71w-dom php71w-pdo php71w-m php71w-mysql php71w-cli php71w-mcrypt httpd mariadb-server
|
檢查php 指令
1 2 3 4 5
| $ php -v PHP 7.1.0 (cli) (built: Dec 3 2016 11:17:43) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies
|
如果能看到上方訊息的話,表示php 已經安裝成功。
啟動相關服務
1 2 3 4
| $ sudo systemctl enable httpd $ sudo systemctl start httpd $ sudo systemctl enable mysqld $ sudo systemctl start mysqld
|
分別讓 資料庫 及 網頁伺服器 在開機後自動啟動。
修改防火牆
1 2
| $ sudo firewall-cmd --permanent --add-port=80/tcp $ sudo firewall-cmd --reload
|
CentOS 預設不允許外部連線,所以先手動打開一個port 給外部IP使用,若您的httpd 是listen 其他port 的話,在更改 –add-port=80/tcp 這部分就可以了。
安裝 composer
1 2
| $ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/bin/composer
|
composer 是php 的套件管理程式,laravel 便是透過composer 來安裝。
安裝 laravel
1 2 3
| $ pwd /home/sjzeng $ composer global require "laravel/installer"
|
設定環境變數
1 2 3
| $ vi ~/.bash_profile add: `:$HOME/.config/composer/vendor/bin` to $path parameter $ source ~/.bash_profile
|
雖然laravel 安裝好了,但當你執行 laravel
這指令時會顯示bash: laravel: command not found
,這是因為還沒告訴作業系統要去哪邊執行laravel。
安裝網站
1 2 3 4
| $ pwd /home/sjzeng $ laravel new blog $ sudo chmod +x /home/sjzeng
|
您可以將網頁目錄安裝在任意地方。這邊有一個坑,作為網站目錄的資料夾,其所屬的所有上層資料夾都必須有執行
的權限,例如筆者選用 /home/sjzeng/blog 作為網站目錄,則
/home/sjzeng/blog
/hom/sjzeng/
/home/
/
上方這四個資料夾都必須有 x
的權限。
更改httpd 設定
1 2 3 4 5 6 7 8
| $ sudo vi /etc/httpd/conf/httpd.conf
DocumentRoot "/home/sjzeng/blog/public" <Directory "/home/sjzeng/blog/public"> AllowOverride All </Directory>
$ sudo systemctl restart httpd
|
預設的 DocumentRoot 是 /var/www/html
,需更改成您安裝網頁的路徑,
在httpd 讀取資料夾的地方 <Directory>
也必須更改成網頁的路徑,如果您在 <Directory>
內有其他設定的話,僅需變更 AllowOverride All
這行即可,其餘不需更改。AllowOverride
是讓httpd 讀取 .htaccess
的設定。
更改 SELinux 設定
1 2 3
| $ pwd /home/sjzeng $ chcon -Rv -t httpd_sys_content_t blog/
|
CentOS 對內部檔案的權限管理非常嚴謹,當其他使用者在看網頁時,CentOS 內其實是有一名叫做 apache
的使用者在瀏覽您的網頁目錄,雖然如此但 apache
也不是什麼檔案都能看,僅能觀看httpd 相關權限的檔案而已,在還沒修改網站權限時你可以透過 ls -Z
來取得目前的權限。
修改前:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ ls -Z drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 app -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 artisan drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 bootstrap -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 composer.json -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 composer.lock drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 config drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 database -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 gulpfile.js -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 package.json -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 phpunit.xml drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 public -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 readme.md drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 resources drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 routes -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 server.php drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 storage drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 tests drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 vendor -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:user_home_t:s0 yarn.lock
|
修改後:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 app -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 artisan drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 bootstrap -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 composer.json -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 composer.lock drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 config drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 database -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 gulpfile.js -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 package.json -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 phpunit.xml drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 public -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 readme.md drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 resources drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 routes -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 server.php drwxrwxrwx. sjzeng sjzeng unconfined_u:object_r:httpd_sys_rw_content_t:s0 storage drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 tests drwxrwxr-x. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 vendor -rw-rw-r--. sjzeng sjzeng unconfined_u:object_r:httpd_sys_content_t:s0 yarn.lock
|
但是 httpd_sys_content_t
僅是瀏覽的權限而已,依據 laravel 的安裝說明,bootstrap/cache
及 storage
這兩個資料夾必須要有寫入的權限,所以需再做以下修改:
1 2 3 4
| $ chcon -Rv -t httpd_sys_rw_content_t blog/storage $ chcon -Rv -t httpd_sys_rw_content_t blog/bootstrap/cache $ sudo chmod -R 777 blog/storage $ sudo chmod -R 777 blog/bootstrap/cache
|
當然如果你不想這麼麻煩的話也可以把SELinux 的檢查關掉,不過非常不建議這招:
1 2 3
| $ sudo setenforce 0 $ sudo chmod -R 777 blog/storage $ sudo chmod -R 777 blog/bootstrap/cache
|
安裝到這邊就大功告成啦,快打開您的瀏覽器輸入IP 試看看吧。