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

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

Laravel 需求

安裝Nginx

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
yum install nginx
systemctl enable nginx
systemctl start nginx
firewall-cmd --permanent --zone=public --add-service=http;firewall-cmd --reload;

安裝php7

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;
yum-config-manager –enable remi-php71
yum install php php-ctype php-json php-openssl php-nette-tokenizer php-pecl-zip php-pdo php-mbstring php-xml

安裝與設定php-fpm

yum install php-fpm
user = nginx
group = nginx
listen = /path/to/unix/socketlisten.owner = nobody
listen.group = nobody
listen.mode = 0666
php_flag[display_errors] = on
php_flag[display_startup_errors] = on
systemctl enable php-fpm
systemctl start php-fpm

設定Nginx

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;
#}
}
nginx -s reload

安裝Laraval

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
composer global require "laravel/installer"
// 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
mv project-name /usr/share/nginx/html/project-name
// 移動至ngnix的網頁根目錄
cd /usr/share/nginx/html
// 查看現在的tag
ls -Z project-name
// 修改laravel專案的tag
chcon -R -t httpd_sys_content_t project-name
chcon -R -t httpd_sys_rw_content_t project-name/storage
server {
#上方略
root /usr/share/nginx/html/project-name/public;
index index.html index.htm index.php;
#下方略
}
nginx -s reload

參考資料

我的職業是軟體工程師。目前正為機車與跨性別的權利努力,也長期關注勞工權益和女性權利等議題。期望社會中的不平等可以越來越少,達到「人生而平等」這崇高的終極目標。

我的職業是軟體工程師。目前正為機車與跨性別的權利努力,也長期關注勞工權益和女性權利等議題。期望社會中的不平等可以越來越少,達到「人生而平等」這崇高的終極目標。