A Complete Tutorial For Setting Up Laravel With Apache On Ubuntu.

Laravel is a popular PHP framework that allows developers to build web applications quickly and easily. In this tutorial, we will guide you through the process of installing and configuring Laravel with Apache on Ubuntu.

Step 1: Install Apache

The first step is to install the Apache web server on your Ubuntu machine. You can do this by running the following command in the terminal:

sudo apt-get update
sudo apt-get install apache2


Once the installation is complete, you can start the Apache service by running the command:

sudo service apache2 start

Step 2: Install PHP and Dependencies

Laravel requires PHP to be installed on your machine. You can install PHP 8 and its dependencies by running the following command:

sudo apt-get install php libapache2-mod-php php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-zip php-curl

Step 3: Configure MySQL

Laravel uses a database to store data, so you will need to install and configure MySQL. To install MySQL, run the following command:

sudo apt-get install mysql-server


Once the installation is complete, you can configure MySQL by running the command:

sudo mysql_secure_installation


This command will prompt you to set the root password for your MySQL server, remove anonymous users, disable remote root login, and delete the test database.


Next, you can log in to the MySQL server using the root account by running the command:

sudo mysql -u root -p


You will be prompted to enter the password for the root user.


Once you are logged in, you can create a new database for your Laravel application by running the following command:

CREATE DATABASE your_database;


You can also create a new user and grant them access to the database by running the following commands:

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;


You should replace your_database, your_username, and your_password with the desired values for your application.


You can then exit the MySQL prompt by running the command:

EXIT;


It is important to note that you should update the .env file with the correct database name, username, and password so that Laravel can connect to the database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password


You have now successfully installed and configured MySQL for use with your Laravel application.

Step 4: Install Composer

Composer is a dependency manager for PHP that is used to install Laravel. You can install Composer by running the following command:

sudo apt-get install composer

Step 5: Install Laravel

Once you have installed Composer, you can use it to install Laravel by running the following command:

composer create-project --prefer-dist laravel/laravel your-project-name


This will create a new directory called "your-project-name" that contains the Laravel files.

Step 6: Configure Apache

The final step is to configure Apache to work with Laravel. You can do this by creating a new virtual host file in the Apache configuration directory. To do this, run the following command:

sudo nano /etc/apache2/sites-available/your-project-name.conf


Then, add the following contents to the file:

<VirtualHost *:80>
    ServerAdmin your-email
    ServerName your-project-name.com
    DocumentRoot /var/www/html/your-project-name/public
    <Directory /var/www/html/your-project-name>
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


Save the file and then enable the new virtual host by running the following command:

sudo a2ensite your-project-name.conf


Finally, restart the Apache service by running the command:

sudo service apache2 restart


That's it! You have now successfully installed and configured Laravel with Apache on Ubuntu. You can now access your Laravel application by navigating to "your-project-name.com" in your web browser.

Please make sure to also check the permission and ownership of the files and folder in your project, and also check the mod_rewrite module is enabled

sudo a2enmod rewrite
sudo service apache2 restart


Additionally, you can also run the following command inside your project directory to give the correct permissions for storage and bootstrap/cache folders

sudo chmod -R 777 storage/
sudo chmod -R 777 bootstrap/cache/


You are now ready to start building your Laravel application!