於CentOS7安裝 Nginx + php7 + php-fpm + Laravel5.6

陳孜穎
10 min readMar 1, 2018

--

Laravel Logo. From: https://commons.wikimedia.org/wiki/File:LaravelLogo.png

Laravel 需求

  • PHP >= 7.1.3
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension

Laravel 官網連結:
https://laravel.com/docs/5.6/installation#installation

安裝Nginx

  1. 預設repo中無Ngnix,要自行新增,請參考Nginx官網。新增/etc/yum.repos.d/nginx.repo ,並加入以下內容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2. 用yum安裝Nginx

yum install nginx

3. 預設開機開啟Nginx

systemctl enable nginx

4. 啟動Nginx

systemctl start nginx

5. 設定防火牆永久允許HTTP

firewall-cmd --permanent --zone=public --add-service=http;firewall-cmd --reload;
  • CentoOS中Nginx的預設root directory在 /usr/share/nginx/html

這時候應該可以透過browser看到Ngnix的預設畫面了

安裝php7

如果直接從Centos7預設的repository安裝,會安裝成php5,要安裝php7得先安裝兩個常用的repo: Remi EPEL

  1. 從官網取得Remi和EPEL rpm的連結,並安裝
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm;
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm;

2. 開啟remi-php72(現在php最新release為7.2.2)

yum-config-manager –enable remi-php71

3. 安裝php及Laravel必須的套件

yum install php php-ctype php-json php-openssl php-nette-tokenizer php-pecl-zip php-pdo php-mbstring php-xml

安裝與設定php-fpm

  1. 安裝php-fpm
yum install php-fpm

2. 設定php-fpm,修改 /etc/php-fpm.d/www.conf

修改user和group,為nginx

user = nginx
group = nginx

如果要改為使用socket file(預設使用127.0.0.1:9000),則需開啟並修改listen設定為下方樣子

listen = /path/to/unix/socketlisten.owner = nobody
listen.group = nobody
listen.mode = 0666

3. 如果在debug階段,想要開啟display error,則修改以下設定

php_flag[display_errors] = on
php_flag[display_startup_errors] = on

4. 預設開機開啟php-fpm

systemctl enable php-fpm

5. 啟動php-fpm

systemctl start php-fpm

設定Nginx

  1. 修改 /etc/nginx/conf.d/default.conf ,以連接php-fpm,以下為範例
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
# autoindex on; // 如果想要開啟顯示資夾目錄的話加入這條
// 建議只在debug階段開啟
try_files $uri $uri/ /index.php?$query_string;
}
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000
// 若使用file socket則改成下面這行
# fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

2. 重新載入Nginx的設定

nginx -s reload

現在可以試著在Nginx的root directory加入index.php,內容如下。看是否能透過瀏覽器取得php的資訊

安裝Laraval

請參考Laraval官網步驟

  1. 安裝composer,這裡安裝成global的方式。詳細見官網
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');";
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;";
php composer-setup.php;
php -r "unlink('composer-setup.php');";
// 移動執行檔至環境路徑資料夾
mv composer.phar /usr/bin/composer

2. 用composer下載Laravel安裝檔

composer global require "laravel/installer"

3. 產生新的Laravel專案

// laravel 的安裝檔位於~/.config/composer/vendor/laravel/installer
cd ~/.config/composer/vendor/laravel/installer
laravel new project-name
// 若不想切換目錄則使用以下指令
cd ~
.config/composer/vendor/laravel/installerㄥlaravel new project-name

4. 移動Laravel資料夾至Nginx的網頁根目錄(如先前有更改過根目錄,請放至更改後的目錄中)

mv project-name /usr/share/nginx/html/project-name

5. 由於CentOS使用SELinux對作業系統進行防護,所以需要給Laravel的資料夾相對應的tag,讓web server可以存取Laravel中的檔案

// 移動至ngnix的網頁根目錄
cd /usr/share/nginx/html
// 查看現在的tag
ls -Z project-name
// 修改laravel專案的tag
chcon -R -t httpd_sys_content_t project-name

6. httpd_sys_content_t 是讓資料夾或檔案可透過http取得,但Laravel在運行中會記錄log及存取本機檔案,所以要再修改其存取本機檔案的資料夾的tag

chcon -R -t httpd_sys_rw_content_t project-name/storage

7. 最後修改Nginx的root directory為Laravel的index.php位置

server {
#上方略
root /usr/share/nginx/html/project-name/public;
index index.html index.htm index.php;
#下方略
}

8. 重新載入Nginx的設定

nginx -s reload

9. 大功告成!現在應該可以透過瀏覽器看到Laravel的預設畫面囉!

參考資料

  1. 在 CentOS 7 安裝 Nginx、PHP-FPM、MariaDB
    http://xyz.cinc.biz/2015/05/centos7-install-nginx-php-fpm-mariadb.html
  2. RHEL / CentOS 7 安裝 PHP 7.0 及 7.1
    https://www.phpini.com/linux/rhel-centos-7-install-php-7
  3. 请给你的 Laravel 一份更好的 Nginx 配置
    https://yii.im/posts/the-right-way-to-set-nginx-for-laravel/
  4. Configuring SELinux Policies for Apache Web Servers
    http://www.serverlab.ca/tutorials/linux/web-servers-linux/configuring-selinux-policies-for-apache-web-servers/
  5. CentOS, Nginx: 403 Forbidden in Laravel’s folder
    https://stackoverflow.com/questions/48986258/centos-nginx-403-forbidden-in-laravels-folder
  6. Laravel — The PHP Framework For Web Artisans
    https://laravel.com

--

--

陳孜穎

我是位軟體工程師。長期關注勞交通和性別平等等議題。期望社會中的不平等可以越來越少,達到「人生而平等」這崇高的終極目標。