Home > Article > Backend Development > Laravel installs in Ubuntu step by step.
To set up Laravel in a fresh Ubuntu, follow these steps:
Run the following commands to update your system's package list and upgrade installed packages:
sudo apt update sudo apt upgrade
For a web server, you can choose either Apache or Nginx. Here, we'll go with Apache:
sudo apt install apache2
Start Apache and enable it to run on startup:
sudo systemctl start apache2 sudo systemctl enable apache2
To check Apache status:
sudo systemctl status apache2
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
Check PHP version:
php -v
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
Check if Composer is installed:
composer -v
Laravel can work with different databases. We'll go with MySQL:
sudo apt install mysql-server
Secure the MySQL installation by running:
sudo mysql_secure_installation
Laravel uses Node.js for compiling front-end assets. Install it with NPM:
sudo apt install nodejs npm
Check versions:
node -v npm -v
Create a virtual host for your Laravel project. First, navigate to the default Apache site directory:
cd /var/www/ sudo mkdir your-laravel-app
Assign correct permissions:
sudo chown -R $USER:$USER /var/www/your-laravel-app
Next, create a new virtual host file:
sudo nano /etc/apache2/sites-available/your-laravel-app.conf
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>
Enable the site and mod_rewrite:
sudo a2ensite your-laravel-app.conf sudo a2enmod rewrite
Restart Apache:
sudo systemctl restart apache2
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 .
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
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
Add your virtual host name to the hosts file:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 your-laravel-app.local
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!