Step-by-Step Guide to Setting Up a LAMP Server for PHP and Laravel Projects

Step-by-Step Guide to Setting Up a LAMP Server for PHP and Laravel Projects

There are a number of steps to do in creating a LAMP (Linux, Apache, MySQL, PHP) server for PHP and Laravel project deployment so here is a thorough guide to take you through. This is supposed to be a guide for an Ubuntu server.

1. Initial Server Setup

  1. Update and Upgrade Packages:

    sudo apt update
    sudo apt upgrade -y
    
  2. Install Essential Packages:

    sudo apt install -y software-properties-common
    

2. Install Apache

  1. Install Apache:

    sudo apt install -y apache2
    
  2. Enable Apache to Start on Boot and Start Apache:

    sudo systemctl enable apache2
    sudo systemctl start apache2
    
  3. Adjust Firewall to Allow Web Traffic:

    sudo ufw allow in "Apache Full"
    

3. Install MySQL

  1. Install MySQL Server:

    sudo apt install -y mysql-server
    
  2. Secure MySQL Installation:

    sudo mysql_secure_installation
    

    Follow the prompts to set a root password and secure your installation.

  3. Create a Database and User for Laravel:

    sudo mysql -u root -p
    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;
    

4. Install PHP

  1. Add PHP Repository:

    sudo add-apt-repository ppa:ondrej/php
    sudo apt update
    
  2. Install PHP and Necessary Extensions:

    sudo apt install -y php libapache2-mod-php php-mysql php-cli php-curl php-zip php-gd php-mbstring php-xml php-pear php-bcmath php-json
    
  3. Configure Apache to Use PHP:

    sudo a2enmod php
    sudo systemctl restart apache2
    

5. Install Composer

  1. Download and Install Composer:
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    sudo chmod +x /usr/local/bin/composer
    

6. Install Laravel

  1. Install Laravel Installer via Composer:

    composer global require laravel/installer
    
  2. Add Composer's Global Bin Directory to Your PATH:

    echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
    source ~/.bashrc
    
  3. Create a New Laravel Project:

    cd /var/www
    composer create-project --prefer-dist laravel/laravel your_project_name
    

7. Configure Apache for Laravel

  1. Create a New Apache Configuration File for Your Laravel Project:

    sudo nano /etc/apache2/sites-available/your_project_name.conf
    
  2. Add the Following Configuration to the File:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/your_project_name/public
    
        <Directory /var/www/your_project_name>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        <Directory /var/www/your_project_name/public>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  3. Enable the New Site and Rewrite Module:

    sudo a2ensite your_project_name.conf
    sudo a2enmod rewrite
    sudo systemctl restart apache2
    

8. Set Permissions and Environment Configuration

  1. Set Proper Permissions:

    sudo chown -R www-data:www-data /var/www/your_project_name
    sudo chmod -R 775 /var/www/your_project_name/storage
    sudo chmod -R 775 /var/www/your_project_name/bootstrap/cache
    
  2. Configure the Environment File:

    cd /var/www/your_project_name
    cp .env.example .env
    nano .env
    

    Update the .env file with your database credentials and other settings.

  3. Generate Application Key:

    php artisan key:generate
    

9. Finalize the Setup

  1. Migrate the Database:

    php artisan migrate
    
  2. Set Directory Permissions for Cache and Storage:

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

Summary

You have a LAMP server at the moment, set up and configured the deployment of PHP and Laravel projects with the installation and configuration of Apache, MySQL, PHP, and even Composer for dependency management.

We have shown a very basic setup in the guide about how to set up a LAMP server. Most likely, you will be more strict in your configuration based on your needs, like working with SSL for secure connections, config of domains, optimization, etc.—always necessary in a production environment.

Vibe Plus 1

Sami Rahimi

Innovate relentlessly. Shape the future..

Recent Comments

Post your Comments (first log in)