How To Set Up Laravel on Ubuntu

How To Set Up Laravel on Ubuntu

How to Set Up Laravel on Ubuntu

Laravel is a powerful PHP framework that simplifies web development. If you’re using Ubuntu, this guide will walk you through installing and setting up Laravel on your server.

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

Step 1: Update Your System

Before installing anything, ensure your system is up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install PHP and Required Extensions

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

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

Verify PHP version:

php -v

Step 3: Install Composer (PHP Dependency Manager)

Laravel uses Composer to manage dependencies. Install it using:

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

Check the version:

composer -V

Step 4: Install MySQL and Configure Database

If you haven’t installed MySQL yet, do so with:

sudo apt install mysql-server -y

Secure MySQL installation:

sudo mysql_secure_installation

Then, log in and create a Laravel database:

sudo mysql -u root -p

Inside MySQL:

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

To install a new Laravel project, navigate to your web directory:

cd /var/www sudo composer create-project laravel/laravel myapp

Set proper ownership:

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 Laravel virtual host:

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>

Save and enable the site:

sudo a2ensite laravel.conf sudo systemctl restart apache2

For Nginx:

Create a new Nginx configuration:

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

Paste the following:

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 the file and enable the site:

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

Step 7: Configure .env File

Go to your Laravel project folder:

cd /var/www/myapp

Edit the .env file to update the database credentials:

nano .env

Modify these lines:

DB_DATABASE=laravel_db DB_USERNAME=laravel_user DB_PASSWORD=your_password

Run the migration:

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

If you’re using a domain, it should work as well.

Step 9: Setup Permissions & Optimize Laravel

Make sure the necessary folders have the correct permissions:

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

Run Laravel optimization commands:

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

Conclusion

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

💬 Have questions or issues? Drop a comment below!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close