首页 > 后端开发 > php教程 > Easy Laravel Deployment on Ubuntu: A Beginner&#s Guide with LEMP Stack

Easy Laravel Deployment on Ubuntu: A Beginner&#s Guide with LEMP Stack

Mary-Kate Olsen
发布: 2024-09-20 06:55:32
原创
639 人浏览过

Easy Laravel Deployment on Ubuntu: A Beginner

Deploying a Laravel application on Ubuntu with the LEMP stack (Linux, Nginx, MySQL, PHP) can seem daunting, but breaking it down step by step makes it manageable. This guide will walk you through the process from server setup to deploying a Laravel application.

Prerequisites:

  • You should have an Ubuntu server (local or cloud, e.g., AWS, DigitalOcean).
  • Basic familiarity with the terminal.
  • A domain name (optional but recommended).

Part 1: Setting Up the LEMP Stack

Step 1: Update the System

Start by ensuring that your server is up to date.

sudo apt update && sudo apt upgrade -y
登录后复制

Step 2: Install Nginx

Nginx will serve your application.

sudo apt install nginx -y
登录后复制

Once installed, you can start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx
登录后复制

You can verify that Nginx is running by visiting your server’s IP address in a browser.

Step 3: Install MySQL

Next, we’ll install the MySQL database server.

sudo apt install mysql-server -y
登录后复制

Secure the MySQL installation:

sudo mysql_secure_installation
登录后复制

This will prompt you to set up a root password and remove insecure defaults.

Step 4: Install PHP

Laravel requires PHP, so let's install it along with some necessary extensions:

sudo apt install php-fpm php-mysql php-cli php-xml php-mbstring php-curl php-zip -y
登录后复制

Verify the PHP installation:

php -v
登录后复制

You should see something like:

PHP 7.x.x (cli) (built: ...)
登录后复制

Part 2: Configuring MySQL for Laravel

Step 1: Log in to MySQL

Log in to the MySQL console as the root user:

sudo mysql
登录后复制

Step 2: Create a Database

Create a new database and user for the Laravel application:

CREATE DATABASE laravel_app;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
登录后复制

Step 3: Test the Database

Ensure that the new database user can connect:

mysql -u laravel_user -p
登录后复制

You’ll be prompted for the password, then enter:

SHOW DATABASES;
登录后复制

You should see laravel_app in the list.


Part 3: Installing Laravel

Step 1: Install Composer

Laravel uses Composer as its dependency manager. Install Composer:

sudo apt install composer -y
登录后复制

Step 2: Create a Laravel Project

Navigate to the directory where you want to install Laravel (e.g., /var/www/):

cd /var/www/
composer create-project --prefer-dist laravel/laravel laravel_app
登录后复制

Step 3: Set Directory Permissions

Laravel requires some directories to be writable by the web server:

sudo chown -R www-data:www-data /var/www/laravel_app
sudo chmod -R 775 /var/www/laravel_app/storage
sudo chmod -R 775 /var/www/laravel_app/bootstrap/cache
登录后复制

Step 4: Configure .env File

In the Laravel project root, open the .env file and configure the database settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=strong_password
登录后复制

Part 4: Configuring Nginx for Laravel

Step 1: Create a New Nginx Server Block

We'll create an Nginx configuration file for the Laravel project.

sudo nano /etc/nginx/sites-available/laravel_app
登录后复制

Add the following configuration to the file:

server {
    listen 80;
    server_name your_domain_or_ip;

    root /var/www/laravel_app/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.x-fpm.sock; # Change this to the correct PHP version.
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
登录后复制

Replace your_domain_or_ip with your actual domain name or server IP address.

Step 2: Enable the Nginx Configuration

Enable the new Nginx configuration by creating a symbolic link to sites-enabled:

sudo ln -s /etc/nginx/sites-available/laravel_app /etc/nginx/sites-enabled/
登录后复制

Step 3: Test and Reload Nginx

Test the Nginx configuration to ensure there are no syntax errors:

sudo nginx -t
登录后复制

If everything is fine, restart Nginx:

sudo systemctl reload nginx
登录后复制

Part 5: Final Steps

Step 1: Run Laravel Migrations

Run the Laravel migrations to set up the database:

cd /var/www/laravel_app
php artisan migrate
登录后复制

Step 2: Access the Application

You should now be able to access the Laravel application by navigating to your server’s IP or domain in the browser. You’ll see the default Laravel welcome page.

Step 3: Enable HTTPS (Optional but Recommended)

If you have a domain, secure your site with Let's Encrypt:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain
登录后复制

Follow the instructions to install an SSL certificate. Certbot will automatically configure Nginx to redirect HTTP traffic to HTTPS.


Part 6: Optional: Setting Up Laravel Queue and Scheduler

Laravel Queue:

Queues handle tasks like sending emails or processing jobs in the background.

  1. Set up a queue driver (e.g., Redis or database).
  2. Run the Laravel queue worker:
   php artisan queue:work
登录后复制

Laravel Scheduler:

Use Laravel's task scheduling feature for tasks like clearing caches, sending daily emails, etc.

  1. Add the Laravel cron entry to your crontab:
   sudo crontab -e
登录后复制

Add the following line:

   * * * * * php /var/www/laravel_app/artisan schedule:run >> /dev/null 2>&1
登录后复制

Conclusion:

You’ve successfully deployed a Laravel application on an Ubuntu server using the LEMP stack. From here, you can continue to develop your application, secure it, and monitor it for performance.

If you encounter any issues, check the Nginx error logs at /var/log/nginx/error.log or Laravel logs at /var/www/laravel_app/storage/logs/laravel.log.

With these steps, you've completed a full hands-on Laravel deployment!

以上是Easy Laravel Deployment on Ubuntu: A Beginner&#s Guide with LEMP Stack的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板