How to Install Laravel on Ubuntu

How to Install Laravel on Ubuntu

How to Install Laravel on Ubuntu

Laravel is one of the most popular PHP frameworks for building web applications. If you’re using Ubuntu, this guide will help you install and set up Laravel on your system.

What You’ll Learn:
✔️ Install PHP, Composer, and Laravel
✔️ Set up a web server (Apache or Nginx)
✔️ Configure Laravel and database
✔️ Run Laravel on Ubuntu

Step 1: Update Your System

Before installing Laravel, update your system packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install PHP and Required Extensions

Laravel requires PHP 8.1+. Install PHP and the necessary extensions:

sudo apt install php-cli php-mbstring php-xml php-bcmath php-curl php-zip unzip curl -y

Verify the PHP installation:

php -v

Step 3: Install Composer

Laravel uses Composer to manage dependencies. Install Composer with:

curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer

Check if Composer is installed:

composer -V

Step 4: Install MySQL and Configure the Database

If you haven't installed MySQL, install it with:

sudo apt install mysql-server -y

Secure your MySQL installation:

sudo mysql_secure_installation

Log in to MySQL and create a database for Laravel:

sudo mysql -u root -p

Run the following SQL commands:

CREATE DATABASE laravel_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES; EXIT;

Step 5: Install Laravel

Navigate to your web directory:

cd /var/www

Use Composer to install Laravel:

sudo composer create-project laravel/laravel myapp

Set proper ownership and permissions:

sudo chown -R www-data:www-data /var/www/myapp sudo chmod -R 775 /var/www/myapp/storage /var/www/myapp/bootstrap/cache

Step 6: Configure Apache or Nginx for Laravel

For Apache:

Enable required modules:

sudo a2enmod rewrite

Create a new virtual host file:

sudo nano /etc/apache2/sites-available/laravel.conf

Paste the following configuration:

<VirtualHost *:80> ServerName yourdomain.com DocumentRoot /var/www/myapp/public <Directory /var/www/myapp> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Enable the site and restart Apache:

sudo a2ensite laravel.conf sudo systemctl restart apache2

For Nginx:

Create a new configuration file:

sudo nano /etc/nginx/sites-available/laravel

Paste the following configuration:

server { listen 80; server_name yourdomain.com; root /var/www/myapp/public; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }

Save and enable the site:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/ sudo systemctl restart nginx

Step 7: Configure Laravel Environment

Navigate to your Laravel project directory:

cd /var/www/myapp

Edit the .env file to update database credentials:

nano .env

Modify these lines:

DB_DATABASE=laravel_db DB_USERNAME=laravel_user DB_PASSWORD=your_password

Run database migrations:

php artisan migrate

Step 8: Run Laravel

Start the Laravel development server:

php artisan serve --host=0.0.0.0 --port=8000

Now, open your browser and visit:

http://your-server-ip:8000

Step 9: Optimize Laravel Performance

Run these optimization commands:

php artisan config:cache php artisan route:cache php artisan view:cache

Set correct permissions:

sudo chmod -R 775 storage bootstrap/cache sudo chown -R www-data:www-data storage bootstrap/cache

Conclusion

🎉 Congratulations! You have successfully installed Laravel on Ubuntu. Now, you can start building amazing web applications! 🚀

💬 Have any questions? Let us know in the comments!

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close