Nginx & Laravel

 Nginx&Laravel

(1) 先建立一個使用者、資料庫給 Laravel 使用

CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'Kyy_y(YL901OzLBh';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;

(2) 安裝 Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

(3) 安裝 Laravel

sudo mkdir /var/www/laravel.local
sudo chown -R willywen:willywen /var/www/laravel.local
cd /var/www/laravel.local
composer create-project --prefer-dist laravel/laravel .

(4) 查看檔案會看到一個叫 .env 的檔案,同時我們給予該資料庫權限

ls -a
sudo chown -R :www-data /var/www/laravel.local/storage/
sudo chown -R :www-data /var/www/laravel.local/bootstrap/cache/
sudo chmod -R 0777 /var/www/laravel.local/storage/
sudo chmod -R 0775 /var/www/laravel.local/bootstrap/cache/

(5) 為 Laravel 生成一個新的密鑰,並且將生成出來的值加到 .env 裡面

sudo php artisan key:generate

(6) 設定 .env 指向資料庫

sudo naono /var/www/laravel.local/.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=Kyy_y(YL901OzLBh

(7) /etc/nginx/sites-available 底下建立一個虛擬服務器,同時連結過去

sudo nano /etc/nginx/sites-available/laravel.local.conf
sudo ln -s /etc/nginx/sites-available/laravel.local.conf /etc/nginx/sites-enabled/


server{

        listen 80;

        listen [::]:80;

        server_name laravel.local;

        root        /var/www/laravel.local/public;

        index       index.php;


        charset utf-8;

        gzip on;

        gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

        location / {

                try_files $uri $uri/ /index.php?$query_string;

        }


        location ~ \.php {

                include fastcgi.conf;

                fastcgi_split_path_info ^(.+\.php)(/.+)$;

                fastcgi_pass unix:/run/php/php8.1-fpm.sock;

        }

        location ~ /\.ht {

                deny all;

        }

}

(8) 接下來我們將 Laravel 連動到資料庫,同時檢查是否有問題,沒問題就重啟服務

php artisan migrate
php artisan db:seed
sudo nginx -t
sudo systemctl restart nginx


留言

這個網誌中的熱門文章

黑暗靈魂3[所有重要物品的取得方式]

黑暗靈魂3[所有姿勢取得方式]

MySQL Replication