---
# System prepended metadata

title: PHP 8 & Laravel 安裝筆記 (搭配 Nginx)
tags: [Nginx, Laravel, PHP]

---

# PHP 8 & Laravel 安裝筆記 (搭配 Nginx)

更新
```
sudo apt update
sudo apt upgrade
```

新增 PPA
```
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
```

為 Nginx 安裝 PHP 8 FPM
`sudo apt install php8.0-fpm`

確認 PHP 8 FPM 正確安裝
`php-fpm8.0 -v`

安裝 PHP 8 擴充及 zip 相關套件
`sudo apt install php8.0-common php8.0-mysql php8.0-xml php8.0-curl php8.0-gd php8.0-imagick php8.0-cli php8.0-dev php8.0-imap php8.0-mbstring php8.0-opcache php8.0-soap php8.0-zip zip unzip -y`

為 Nginx 設定 PHP 8
`sudo vi /etc/php/8.0/fpm/php.ini`
修改
```
upload_max_filesize = 32M 
post_max_size = 48M 
memory_limit = 256M 
max_execution_time = 600 
max_input_vars = 3000 
max_input_time = 1000
```

重新啟動 PHP FPM 使更改生效
```
sudo php-fpm8.0 -t 
sudo service php8.0-fpm restart
```

---

安裝 Nginx
`sudo apt install nginx`
配置防火牆
`sudo ufw allow ssh`
`sudo ufw allow 'Nginx Full'` // opens both port 80 and port 443
啟動 Nginx
`sudo systemctl start nginx`
若有出現錯誤 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
`sudo apachectl stop`
查看 Nginx 運行狀態
`systemctl status nginx.service`
若為 status: inactive
`sudo ufw enable` 啟用
查看防火牆運行狀態
`sudo ufw status`
就能看到以下畫面
![](https://i.imgur.com/qICAb6u.png)

---

將 Nginx 升級到 PHP 8
`sudo vi /etc/nginx/sites-available/your.conf`
如 `sudo vi /etc/nginx/sites-available/default`

修改 location / 內
`try_files $uri $uri/ /index.php?$query_string;`

修改 fastcgi_pass
`fastcgi_pass unix:/run/php/php8.0-fpm.sock;`

重新啟動 Nginx
```
sudo nginx -t
sudo service nginx restart
```

安裝 Composer
基本上按照[官方教學](https://getcomposer.org/download/)沒什麼問題
在家目錄下操作
~~php composer-setup.php~~
**改成**
`php composer-setup.php --install-dir=/usr/local/bin --filename=composer`

使用 Composer 安裝 Laravel
`composer global require laravel/installer` 之後
laravel 位在 `$HOME/.config/composer/vendor/bin`，需要加入 PATH
`vi ~/.profile` 在最後一行加入
`PATH=$PATH:$HOME/.config/composer/vendor/bin`
然後 `source ~/.profile`
就可以直接使用 `laravel` 指令囉

參考
https://tw511.com/a/01/23398.html

###### tags: `PHP` `Nginx` `Laravel`