Introduction
Laravel is a widely-used, free, and open-source PHP framework designed for developing web applications. Built on the Model-View-Controller (MVC) architecture, it simplifies the development process with its extensive features, enhancing speed, ease, and convenience. Laravel's popularity stems from its flexibility, vast community support, and frequent updates. It provides developers with accessible tools for authentication, routing, database management, and more.
This tutorial will guide you through installing Laravel on Ubuntu (starting from Ubuntu 16.04).
1. Update Your System
- First, update your system repositories with:
apt update
- If there are any upgradable packages, execute:
apt upgrade
2. Install Apache
- To run Laravel, you must set up Apache on your server. Install it with:
apt install apache2
- Once installed, start Apache:
systemctl start apache2
- Verify Apache is running with:
systemctl status apache2
3. Create a Database
- A database is necessary for Laravel. If MySQL isn't already installed, use:
apt install mysql-server
- Connect to MySQL as the root user (the default password is blank, so just press Enter):
mysql -u root -p
- Create a new database for Laravel:
CREATE DATABASE my_laravel;
- Create a new database user (replace
your_user
with your desired username) and set a password:
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'user_password';
- Grant this user all privileges on the Laravel database:
GRANT ALL ON my_laravel.* TO 'your_user'@'localhost';
exit
4. Install PHP
- Laravel requires PHP. Install PHP and its extensions with:
apt install php libapache2-mod-php php-mbstring php-cli php-bcmath php-json php-xml php-zip php-pdo php-common php-tokenizer php-mysql
apt-get install php-curl
- Check if PHP is installed:
php --version
5. Install Composer
- Composer, a PHP package manager, is necessary. Install it with:
curl -sS https://getcomposer.org/installer | php
- Move the downloaded file to the appropriate directory:
mv composer.phar /usr/local/bin/composer
- Grant execute permissions:
chmod +x /usr/local/bin/composer
- Verify Composer's installation:
composer --version
6. Install Laravel
- Change the working directory to
/var/www/html
:
cd /var/www/html
- Install Laravel using Composer:
composer create-project laravel/laravel laravelapp
7. Set Laravel Permissions
- Assign ownership of the
laravelapp
directory to the web server:
chown -R www-data:www-data /var/www/html/laravelapp
- Set the appropriate permissions:
chmod -R 775 /var/www/html/laravelapp/storage
8. Verify Laravel Installation
- Navigate to the Laravel directory:
cd laravelapp
- Run the following command to check the installation:
php artisan
- You should see information about the installed Laravel version.
Configure Apache for Laravel
- To host your Laravel site, configure Apache. Open the
laravel.conf
file:
vi /etc/apache2/sites-available/laravel.conf
- Add these lines, replacing
yourdomain.ltd
with your actual domain:
<VirtualHost *:80>
ServerName yourdomain.ltd
ServerAdmin admin@yourdomain.ltd
DocumentRoot /var/www/html/laravelapp/public
<Directory /var/www/html/laravelapp>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and exit the editor (press Esc
, type :wq
, and hit Enter).
Enable the site configuration:
a2ensite laravel.conf
systemctl reload apache2
- Enable the
rewrite
module:
a2enmod rewrite
systemctl restart apache2
- After configuring Apache, you can access your Laravel site by entering your domain in a web browser, displaying the default Laravel page.
Removing Laravel
- To uninstall Laravel, remove the Composer project by running:
rm /usr/local/bin/composer
- Laravel will then be removed from your server.