Skip to main content

快取指令

  1. 安裝laravel

php & composer
sudo apt install php composer php-cli php-common php-mbstring php-xml php-zip php-mysql php-json php-bcmath php-curl php-gd php-tokenizer php8.1-xml php-fpm mariadb-server nginx

這裡要是需要或習慣安裝apache or nginx,nginx需要使用php-fpm

php -v
sudo apt install curl php-cli php-mbstring git unzip
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
composer --version
install laravel
composer create-project --prefer-dist laravel/laravel your-project-name
cd your-project-name

install apache2 or nginx

apache
sudo vi /etc/apache2/sites-available/your-project-name.conf
<VirtualHost *:80>
    ServerName your-domain-or-ip
    DocumentRoot /var/www/html/your-project-name/public
    <Directory /var/www/html/your-project-name>
        AllowOverride All
    </Directory>
</VirtualHost>
sudo a2enmod rewrite

Enable the virtual host:

sudo a2ensite your-project-name.conf

Restart Apache for the changes to take effect:

sudo systemctl restart apache2
nginx
sudo vi /etc/nginx/sites-available/your-project-name
server {
    listen 80 default_server;
    server_name your-domain-or-ip;
    return 301 https://$server_name$request_uri;
    }

server {
    listen 443 ssl default_server;
    server_name your-domain-or-ip;

    include snippets/ssl;
    include snippets/ssl;

    root /var/www/html/your-project-name/public;
    index index.php;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /var/www/html;
    }
    
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #      With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #

        add_header X-XSS-Protection "1; mode=block";
#       add_header 'Access-Control-Allow-Origin' *;
#       add_header 'Access-Control-Allow-Credentials' true;
#       add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
#       add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;

        location ~*  \.(jpg|jpeg|gif|png|svg)$ {
                expires 365d;
        }

        location ~*  \.(pdf|css|html|js|swf)$ {
                expires 2d;
        }

        location ~ /\.ht {
                deny all;
        }

        location ~ ^/\.user\.ini {
                deny all;
        }

       location ~ /.well-known/acme-challenge {
        root /var/www/html;
               allow all;
        }

        location ~ /\.ht {
                deny all;
        }
}

Replace your-domain-or-ip with your actual domain name or server IP address.
Enable the Nginx server block:

sudo ln -s /etc/nginx/sites-available/your-project-name /etc/nginx/sites-enabled/

Test the Nginx configuration for any syntax errors:

sudo nginx -t

Restart Nginx for the changes to take effect:

sudo systemctl restart nginx

MariaDB 資料庫

在安裝好 MariaDB 資料庫後,使用前建議先調整一下安全性的設定:

# MariaDB/MySQL 資料庫安全性設定

sudo mysql_secure_installation

使用 root 登入 MariaDB/MySQL 資料庫

sudo mysql -u root -p

建立 laravel 資料庫

CREATE DATABASE laravel;
CREATE USER `user`@`localhost` IDENTIFIED BY 'yourpassword';
GRANT ALL ON laravel.* TO `user`@`localhost`;
FLUSH PRIVILEGES;

編輯 Laravel 專案目錄中的 .env 設定檔,將資料庫的相關資訊填入其中:

DB_DATABASE=laravel
DB_USERNAME=user
DB_PASSWORD=yourpassword

除了 .env 的設定檔之外,也可以直接編輯 config/database.php 中的資料庫設定。

php artisan serve 伺服器   #開啟服務,預設8000
php artisan migrate       #建立使用資料庫
php artisan about         #快速瀏覽環境設定

Configure Laravel

cp .env.example .env

Generate a new application key

php artisan key:generate

Set the appropriate permissions on Laravel directories

sudo chown -R www-data:www-data /var/www/html/your-project-name/storage
sudo chmod -R 775 /var/www/html/your-project-name/storage
Laravel Breeze
composer require laravel/breeze --dev      # 安裝Breeze

安裝npm跟nodejs,請注意版本問題

sudo apt install npm
sudo apt install nodejs

##ubuntu 系統函式庫的nodejs版本通常會太舊,可以用下方方式處理,請問注意版本 ,或可以從https://nodejs.org/en下載##

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - 
sudo apt-get install -y nodejs
php artisan breeze:install
php artisan migrate
npm install

直接運作

npm run dev