如何为客户在本地主机部署已完成的Laravel项目
Hey there! No stress at all—setting up a Laravel project locally for the first time can feel a bit overwhelming, but let’s walk through this step by step, nice and easy. I’ll make sure every part makes sense:
Laravel needs a few basic tools to run, so let’s confirm these are set up first:
- Check PHP version: Laravel 9+ requires PHP 8.0 or higher. Open your terminal (CMD/PowerShell for Windows, Terminal for Mac/Linux) and run
php -vto check your version. If it’s too low, I recommend installing XAMPP or WAMP—they come with pre-configured PHP, Apache, and MySQL, which is super beginner-friendly. - Install Composer: Composer is Laravel’s dependency manager. Run
composer -Vin your terminal to see if it’s installed. If not, grab the installer from the official Composer site, follow the wizard, and make sure to check the box that adds Composer to your system PATH (so you can use it anywhere in the terminal). - Prepare a database: XAMPP/WAMP includes MySQL. Start MySQL from their control panel, then open phpMyAdmin (usually at
http://localhost/phpmyadmin). Create an empty database and note down its name, the username (default isroot), and password (XAMPP defaults to blank, WAMP might be blank orroot).
- Extract the project zip file your client gave you to a convenient location: If using XAMPP, put it in the
htdocsfolder (e.g.,htdocs/your-client-project); if not using a pre-built server, just pick a folder you can easily find. - Copy the environment config file: There’s a
.env.examplefile in the project root. Copy it and rename the copy to.env—this is Laravel’s core config file, and it’s required. You can do this via terminal:- Linux/Mac:
cp .env.example .env - Windows CMD:
copy .env.example .env
- Linux/Mac:
Open the .env file with an editor like VS Code or Notepad++, then update the database section to match the info you noted earlier:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your-database-name DB_USERNAME=root DB_PASSWORD=your-database-password (leave blank if there’s none)
Also, set the APP_URL to match your setup: If using Apache, use APP_URL=http://localhost/your-client-project; if using Laravel’s built-in server, use APP_URL=http://localhost:8000.
Go back to your terminal, navigate to the project root folder (e.g., cd htdocs/your-client-project), then run:composer install
This will download all the PHP packages Laravel needs. It might take a few minutes the first time—if it’s slow, you can switch to a domestic mirror first by running composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/.
Run this command in the terminal:php artisan key:generate
This creates an encryption key for your app—Laravel can’t run without it, so this step is non-negotiable.
There are two ways to get your database set up:
- Use Laravel migrations: Run
php artisan migratein the terminal—this will automatically create all the tables the project needs. - Import a database backup: If your client provided a SQL backup file, open phpMyAdmin, select your database, go to the "Import" tab, and upload the backup file. This is usually faster if the project already has existing data.
Now you can launch the app with one of these options:
- Laravel’s built-in development server: Run
php artisan servein the terminal. Once it says "Server running on http://localhost:8000", open that URL in your browser. - Apache/Nginx (XAMPP/WAMP): Make sure Apache is running in your XAMPP/WAMP control panel, then visit
http://localhost/your-client-projectdirectly in your browser.
- Permission errors: If you get errors about the
storageorbootstrap/cachefolders, runchmod -R 775 storage bootstrap/cacheon Linux/Mac. For Windows, right-click those folders, go to Properties > Security, and give "Everyone" read/write permissions. - Missing PHP extensions: If Composer complains about missing extensions (like
fileinfoormbstring), open your php.ini file (found in XAMPP/WAMP’s PHP folder), remove the semicolon;from the start of the extension line, then restart Apache.
内容的提问来源于stack exchange,提问作者user9653376

