Laravel installs in Ubuntu step by step.

PHPz
Release: 2024-09-07 06:34:32
Original
471 people have browsed it

Laravel installs in Ubuntu step by step.

To set up Laravel in a fresh Ubuntu, follow these steps:

Step 1: Update and Upgrade System Packages

Run the following commands to update your system's package list and upgrade installed packages:

sudo apt update
sudo apt upgrade
Copy after login

Step 2: Install Apache (or Nginx)

For a web server, you can choose either Apache or Nginx. Here, we'll go with Apache:

sudo apt install apache2
Copy after login

Start Apache and enable it to run on startup:

sudo systemctl start apache2
sudo systemctl enable apache2
Copy after login

To check Apache status:

sudo systemctl status apache2
Copy after login

Step 3: Install PHP and Extensions

Laravel requires PHP, so install PHP along with necessary extensions:

sudo apt install php php-cli php-mbstring php-xml php-bcmath php-tokenizer php-json php-curl php-zip php-mysql libapache2-mod-php
Copy after login

Check PHP version:

php -v
Copy after login

Step 4: Install Composer

Composer is required for managing Laravel dependencies. To install it, run:

sudo apt install curl
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Copy after login

Check if Composer is installed:

composer -v
Copy after login

Step 5: Install MySQL (or PostgreSQL)

Laravel can work with different databases. We'll go with MySQL:

sudo apt install mysql-server
Copy after login

Secure the MySQL installation by running:

sudo mysql_secure_installation
Copy after login

Step 6: Install Node.js and NPM

Laravel uses Node.js for compiling front-end assets. Install it with NPM:

sudo apt install nodejs npm
Copy after login

Check versions:

node -v
npm -v
Copy after login

Step 7: Set Up Virtual Host (for Apache)

Create a virtual host for your Laravel project. First, navigate to the default Apache site directory:

cd /var/www/
sudo mkdir your-laravel-app
Copy after login

Assign correct permissions:

sudo chown -R $USER:$USER /var/www/your-laravel-app
Copy after login

Next, create a new virtual host file:

sudo nano /etc/apache2/sites-available/your-laravel-app.conf
Copy after login

Add the following configuration inside the file:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your-laravel-app.local
    DocumentRoot /var/www/your-laravel-app/public
    <Directory /var/www/your-laravel-app>
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Copy after login

Enable the site and mod_rewrite:

sudo a2ensite your-laravel-app.conf
sudo a2enmod rewrite
Copy after login

Restart Apache:

sudo systemctl restart apache2
Copy after login

Step 8: Install Laravel

Navigate to the /var/www/your-laravel-app directory and install Laravel using Composer:

cd /var/www/your-laravel-app
composer create-project --prefer-dist laravel/laravel .
Copy after login

Step 9: Configure Database Connection

Open .env file in your Laravel project and update the database configuration:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
Copy after login

Step 10: Set Permissions

Set the correct permissions for the storage and bootstrap/cache directories:

sudo chown -R www-data:www-data /var/www/your-laravel-app
sudo chmod -R 775 /var/www/your-laravel-app/storage
sudo chmod -R 775 /var/www/your-laravel-app/bootstrap/cache
Copy after login

Step 11: Update Hosts File

Add your virtual host name to the hosts file:

sudo nano /etc/hosts
Copy after login

Add the following line:

127.0.0.1   your-laravel-app.local
Copy after login

Step 12: Run Laravel Project

Access your Laravel project in the browser by visiting http://your-laravel-app.local.

This will give you a working Laravel environment on your Ubuntu system!

The above is the detailed content of Laravel installs in Ubuntu step by step.. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template