My Email: 2421547970@qq.com

Copyright© 2024 All Rights Reserved.

苏ICP备2024148111号-1

本博客后端用laravel框架,前端用html、css等。以下代码为在linux系统(debian12.0)创建laravel项目。

# 安装php
sudo apt install php8.3
# 安装composer
php -r "readfile('https://getcomposer.org/installer');" | php


# 配置环境变量
vim ~/.zshrc
source ~/.zshrc
export PATH=/usr/local/bin:$PATH
# 转移composer.phar文件(全局调用)
##通过composer就可以使用composer了,不管是不是root用户,都要加上sudo
sudo mv composer.phar /usr/local/bin/composer
# 检测是否安装成功
composer --version

# 更换镜像   有时候有的镜像在构建laravel项目时无法成功
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/             这个有时候不行
composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/composer/     
composer config -g repo.packagist composer https://mirrors.ustc.edu.cn/composer/

# 查看镜像源
composer config -l -g

# 安装docker desktop
## 安装docker desktop之前,必须安装gnome-terminal
sudo apt-get update
sudo apt install gnome-terminal

### 安装docker引擎
1. 设置 Docker 的 apt 存储库
###Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

###Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
2. 安装docker包
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

### 下载最新版本的deb包(版本会更新,下载最新的版本即可)
wget https://desktop.docker.com/linux/main/amd64/137060/docker-desktop-4.27.2-amd64.deb?utm_source=docker&utm_medium=webreferral&utm_campaign=docs-driven-download-linux-amd64&_gl=1*tpz2cp*_ga*MTQ1NzQxODY1Mi4xNzA3NjQ3Nzk0*_ga_XJWPQMJYHQ*MTcwNzc5NjczMC40LjEuMTcwNzc5ODI0Ny41OS4wLjA.

### 安装包(version arch 根据上面下载的包确定)
sudo apt-get update
sudo apt-get install ./docker-desktop-4.28.0-amd64.deb

sudo apt-get install ./docker-desktop--.deb

### 查看docker的一些版本参数信息
docker compose version 
docker --versino 
docker version

### 更新docker desktop (重新下载最新版本的包)
sudo apt-get install ./docker-desktop--.deb


#################在 Linux 使用 Laravel Sail##########################
# 创建项目
curl -s https://laravel.build/lab | sudo bash
cd lab
# 安装sail (不需要 已经自动安装好)
composer require laravel/sail --dev
## 发布 Sail 的 docker-compose.yml 文件到你应用程序的根目录
php artisan sail:install
# 启动docker容器
sudo ./vendor/bin/sail up -d
## 出现报错(0.0.0.0:80 这个端口被占用,查看后发现apache2占用)
Attaching to laravel.test-1, mailpit-1, meilisearch-1, mysql-1, redis-1, selenium-1
Error response from daemon: driver failed programming external connectivity on endpoint example-app-laravel.test-1 (a1e863833b430647ad108de456d6fe47e4de4cc40954fc718a6f462e7d49bc62): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use
## 解决方法
sudo service apache2 stop

## 如果出现这样的报错
Error response from daemon: driver failed programming external connectivity on endpoint blog-mysql-1 (ac58b86f30e9d8929bd0c769d55d2b628d0d940d96759260d72d4d137bec08e9): Bind for 0.0.0.0:3306 failed: port is already allocated
## 解决方法
lsof -i :3306
sudo kill -9 
# 结束docker容器
sudo ./vendor/bin/sail down


# 通过 本地ip 打开网页
## 会出现报错
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log:
## 解决办法(我通过以下步骤之后,网页恢复正常)
chowm -R yihui:yihui ~/lab
sudo chmod -R 777 ~/storage    ##或者      sudo chmod -R 777 *

上述是我使用本地安装的方法构建的laravel项目,必须要运行docker之后网站才能正常运行。所以下一步要用nginx进行代理。

# nginx代理
## 将laravel项目移到/var/www目录下
sudo mv ~/laravel /var/www/laravel
## 开放权限
sudo chown -R www-data.www-data /var/www/laravel
## 在/etc/nginx/sites-available目录下创建 laravel.conf
server {
    listen 80;
    server_name server_domain_or_IP;
    root /var/www/travellist/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_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
## 将该文件软链接到/etc/nginx/sites-available目录
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
## 检测
sudo /usr/sbin/nginx -t
## 重启ngnix
sudo systemctl reload nginx

然后就可以正常运行网站了。