Laravel 應用程式佈署

工作坊

大家好,我是芥龍

現任希幔數位後端工程師

Muzik Online

PHP 是世界上最好的語言

今日大綱

  • 執行環境簡介
  • 環境佈署
    • 開發時期
    • 單機產品環境

基礎環境開始

PHP Runtime

Database

Web Server

PHP Runtime

  • 版本 >= 7.1
    • 建議使用 7.2
  • 一些些 extension
  • 可安裝 composer

Database

  • MySQL
    • MySQL
    • MariaDB
    • Percona
  • PostgresQL
  • MS SQL
  • Oracle SQL

Web Server

  • Nginx
  • Apache

連線的生命周期

HTML File

Client

Server

I want index.html

Here is index.html

PHP File

Client

Server

PHP FPM

I want index.php

.php?

By pass to PHP-FPM

Return the result

index.php

Here is index.php

PHP File with DB

Client

Server + PHP FPM

Database

I want index.php

I need some data from database

Here is the data you want

Here is index.php

開發時期環境設定

選擇

  • 官方
    • Homestead
    • Valet
  • 社群
    • Valet Linux
    • Valet Windows
    • wagon
    • Laradock
  • 雲端
    • c9.io
    • codeanywhere

身為一個 Geek

總是自己來比較安心

執行 Laravel 必須軟體

PHP

是的,就這樣,沒有其它的

那其它的東西?

  • Database?
  • Web Server?

實作

佈署開發時期環境

步驟

  • 找一個 Unix Like 環境
  • 安裝 PHP 及所需的 extension
  • 安裝 composer
  • (Optional) 安裝 Docker
    • 啟動 MySQL Container
    • 啟動 Redis Container

找一個 Unix Like 環境

  • 推薦:Digital Ocean
    • Ubuntu 18.04
  • 生成 ssh keypair
$ ssh-keygen -t rsa -b 4096 -C "username@hostname"
  • 以  SSH 連線到主機
$ ssh root@{ip-address}
  • 更新系統
> apt update
> apt upgrade

安裝 PHP 及其 extension

> apt install php7.2 \
    php7.2-cli \
    php7.2-common \
    php7.2-mbstring \
    php7.2-xml \
    php7.2-mysql \
    php7.2-zip
    

安裝 composer 及

laravel installer

> apt install composer
> composer g require "laravel/installer"
> echo 'export PATH="$HOME/.config/composer/vendor/bin:$PATH"' \ 
    >> ~/.bashrc

> laravel new test-laravel
> cd test-laravel
> php artisan serve --host 0.0.0.0

安裝 Docker

並啟動 MySQL 及 Redis

> apt install docker.io
> docker run --name mariadb \
    -p 3306:3306 \
    -d \
    -e MYSQL_ROOT_PASSWORD=root \
    -e MYSQL_DATABASE=test \
    -e MYSQL_USER=homestead \
    -e MYSQL_PASSWORD=secret \
    mariadb \
    --character-set-server=utf8mb4 \
    --collation-server=utf8mb4_unicode_ci
> docker run --name redis \
    -p 6379:6379 \
    -d \
    redis

試玩 nfu-workshop 專案

> git clone https://github.com/chivincent/nfu-workshop.git
> cd nfu-workshop/

> composer install
> yarn install 
> yarn run dev

> php artisan migrate
> php artisan serve --host 0.0.0.0

實作

佈署單機產品時期環境

步驟

  • 建立 CentOS 7 伺服器
  • 安裝所需軟體
    • Nginx
    • PHP
    • MySQL
  • 佈署系統
    • 基礎
    • 佇列服務
    • 定時任務服務

設定 CentOS

  • 建立使用者
> useradd {your-user-name}
> passwd {your-user-name}
> usermod -aG wheel {your-user-name}
  • 以新使用者身份登入
> cd /home/{your-user-name}
> mkdir .ssh
> echo "{your-ssh-public-key}" > authorized_keys

$ ssh {your-user-name}@{ip-address}

安裝 Nginx

  • 加入 Nginx 官方軟體來源並安裝
    • http://bit.ly/nginx-install
  • 啟動 nginx 作為系統服務
    • systemctl start nginx
    • systemctl enable nginx
  • 確定 nginx 安裝
    • curl localhost
    • 直接 Access IP Address

安裝 PHP

  • 加入 PHP Remi 軟體源
    • http://bit.ly/remi-php72-install
  • 安裝 PHP 及 extensions
    • php
    • php-pdo php-mysql
    • php-mbstring
    • php-xml
    • php-zip
    • php-fpm
    • php-opcache
  • 安裝 composer
    • http://bit.ly/php-composer-install

安裝 MariaDB

  • 加入 MariaDB 官方軟體源並安裝
    • http://bit.ly/mariadb-install
  • 啟動 MariaDB
    • systemctl start mariadb
    • systemctl enable mariadb
  • 設定 MariaDB
    • mysql_secure_installation
    • 建立本地 User homestead

設定 Nginx 及 php-fpm

  • 建立使用者 www
  • 更改 nginx 啟動使用者
    • /etc/nginx/nginx.conf
  • 更改 php-fpm 啟動使用者
    • /etc/php-fpm.d/www.conf

加入應用程式

  • 使用 git 取得應用程式
    • 置於 /var/www 下
  • composer install 時的注意事項
  • 為應用程式加入 cache
    • route:cache
    • config:cache
    • view:cacne
  • 注意權限的設定
    • storage
    • bootstrap/cache

設定 Nginx

  • 建立 /etc/nginx/conf.d/nfu-workshop.conf
server {
    listen 80;
    server_name nfu-workshop.test;
    root /var/www/nfu-workshop/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

設定 SELinux

> sudo chcon -Rt httpd_sys_content_t \
    /var/www/nfu-workshop
> sudo chcon -Rt httpd_sys_rw_content_t \
    /var/www/nfu-workshop/storage
> sudo chcon -Rt httpd_sys_rw_content_t \
    /var/www/nfu-workshop/bootstrap/cache
> sudo /usr/sbin/setsebool httpd_can_network_connect=1

設定 HTTPS

  • 使用 certbot
    • http://bit.ly/certbot-install

設定 Queue

  • 設定 systemd
# File Path: /etc/systemd/system/laravel-queue-worker.service
[Unit]
Description=Laravel queue worker

[Service]
User=www
Group=www
Restart=on-failure
ExecStart=/usr/bin/php /var/www/nfu-workshop/artisan queue:work --sleep=3 --tries=3

[Install]
WantedBy=multi-user.target

設定 Cron Job

  • crontab -e
* * * * * /usr/bin/php /var/www/nfu-workshop/artisan schedule:run >> /dev/null 2>&1

Laravel 應用程式佈署工作坊 @ 虎尾科技大學

By chivincent

Laravel 應用程式佈署工作坊 @ 虎尾科技大學

  • 1,093