Efficient Steps To Install Laravel on Rocky Linux 8

Posted on

Efficient Steps To Install Laravel on Rocky Linux 8

Efficient Steps To Install Laravel on Rocky Linux 8

In this guide from Orcacore, we will walk you through the process of installing Laravel on Rocky Linux 8 and securing your Laravel application.

Laravel is a robust and user-friendly open-source PHP framework. Built upon the model-view-controller (MVC) architectural pattern, Laravel promotes structured and pragmatic web application development by leveraging existing components from various frameworks.

To successfully install Laravel on Rocky Linux 8, certain prerequisites must be met. Let’s outline them.

1. Requirements for Laravel Setup

First, ensure you are logged into your server as a non-root user with sudo privileges and have configured a basic firewall. Refer to our guide on Initial Server Setup with Rocky Linux 8 for detailed instructions.

Second, a LEMP stack (Linux, Nginx, MySQL/MariaDB, PHP) must be installed on your server. Consult our guide on How To Install LEMP Stack on Rocky Linux 8 for assistance.

Finally, you’ll need a domain name pointing to your server’s IP address.

With these requirements in place, proceed with the following steps to install Laravel on Rocky Linux 8.

2. Install PHP Extensions for Laravel

Begin by installing the necessary PHP extensions and required packages using the following command:

sudo dnf install php-common php-xml php-mbstring php-json php-zip curl unzip -y

Edit php-fpm Configuration File

Next, modify the php-fpm configuration file. Open the file using your preferred text editor (e.g., vi):

sudo vi /etc/php-fpm.d/www.conf

Locate the following lines, uncomment them by removing the leading semicolon (;), and change them to use nginx:

listen.owner = nginx
listen.group = nginx

Save and close the file after making the changes.

Edit php.ini Configuration File

Now, edit the PHP configuration file:

sudo vi /etc/php.ini

Modify the following lines, setting your desired timezone and uncommenting them by removing the leading semicolon (;):

date.timezone = America/New_York
cgi.fix_pathinfo=1

Save and close the file.

3. Install Composer for Laravel Setup

We will be installing Laravel using Composer, a dependency manager for PHP. Install Composer on Rocky Linux 8 with the command below:

sudo curl -sS https://getcomposer.org/installer | php

Upon successful installation, you’ll see output similar to this:

Install Composer to install Laravel on Rocky Linux 8

Move the Composer binary to the system path:

sudo mv composer.phar /usr/local/bin/composer

Set the appropriate permissions:

sudo chmod +x /usr/local/bin/composer

Verify your Composer installation by checking its version:

composer --version
**Output**
Composer version 2.4.4 2022-10-27 14:39:29

4. Install Laravel on Rocky Linux 8 with Composer

Now you can proceed to install Laravel on your server.

Navigate to the Nginx web root directory:

sudo cd /var/www/html/

Install Laravel using Composer:

sudo composer create-project --prefer-dist laravel/laravel laravel

After the installation is complete, you will receive the following output:

**Output**
   INFO  Application key set successfully.

Set the correct permissions and ownership for the Laravel directory:

# sudo chown -R nginx:nginx /var/www/html/laravel/
# sudo chown -R nginx:nginx /var/www/html/laravel/storage/
# sudo chown -R nginx:nginx /var/www/html/laravel/bootstrap/cache/
# sudo chmod -R 0777 /var/www/html/laravel/storage/
# sudo chmod -R 0775 /var/www/html/laravel/bootstrap/cache/

5. Configure Nginx Config File for Laravel

Create an Nginx configuration file for Laravel:

sudo vi /etc/nginx/conf.d/laravel.conf

Add the following content, replacing domain-name with your actual domain name:

server {
       listen 80;
       server_name domain-name;
       root        /var/www/html/laravel/public;
       index       index.php;
       charset utf-8;
       gzip on;
    gzip_types text/css application/javascript text/javascript application/x-javascript  image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ .php {
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/www.sock;
        }
        location ~ /.ht {
                deny all;
        }
}

Save and close the file.

Verify the Nginx configuration for errors:

nginx -t
**Output**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx and PHP-FPM to apply the changes:

# sudo systemctl restart php-fpm
# sudo systemctl restart nginx

Configure Firewall

Allow ports 80 and 443 through the firewall:

# sudo firewall-cmd --zone=public --permanent --add-service=http
# sudo firewall-cmd --zone=public --permanent --add-service=https

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

6. Access Laravel Web GUI

Access your Laravel application through your web browser by entering your domain name or server’s IP address:

http://domain-name
Larave; Web GUI

7. Secure Laravel with Let’s Encrypt on Rocky Linux 8

It is crucial to enable SSL on your Laravel website to secure the connection. Let’s Encrypt provides free SSL certificates for obtaining, renewing, and managing SSL/TLS certificates for your domain.

Install the Certbot client on Rocky Linux 8:

sudo dnf install epel-release -y
sudo dnf install certbot -y

Download the Let’s Encrypt SSL certificate for your Laravel domain:

sudo certbot --nginx -d domain-name

You’ll be prompted to provide your email address and accept the terms of service. Choose whether to redirect HTTP traffic to HTTPS by typing ‘2’ and pressing Enter.

Your Laravel website is now secured with Let’s Encrypt SSL on Rocky Linux 8.

Access it securely via:

https://domain-name

Conclusion

You have successfully installed, configured, and secured Laravel on Rocky Linux 8. Utilizing Laravel on Rocky Linux 8 offers a secure, stable, and high-performance environment for web applications. With its elegant syntax, built-in security features, scalability, and efficient development tools, Laravel is perfectly suited for modern PHP application development.

Alternative Solutions for Installing Laravel on Rocky Linux 8

While using Composer is the recommended approach for installing Laravel, two other methods can be considered, although they might be less common or suitable for specific scenarios:

1. Using a Pre-built Virtual Machine or Docker Container:

  • Explanation: Instead of manually installing all dependencies and configuring the server, you can use a pre-built virtual machine (VM) or Docker container that already has Laravel and its dependencies installed. This method simplifies the setup process and ensures consistency across different environments. Tools like Vagrant (for VMs) and Docker can be used to manage these environments.

  • Advantages: Faster setup, consistent environment, reduced manual configuration.

  • Disadvantages: Larger initial download size, potential overhead from virtualization, might require familiarity with virtualization technologies.

  • Code Example (Docker):

    First, create a Dockerfile in your project directory:

    FROM php:8.1-fpm-alpine
    
    # Install extensions
    RUN docker-php-ext-install pdo pdo_mysql
    
    # Install composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    
    # Set working directory
    WORKDIR /var/www/html
    
    # Copy existing application source code
    COPY . .
    
    # Install dependencies
    RUN composer install
    
    # Expose port 9000 and start php-fpm server
    EXPOSE 9000
    CMD ["php-fpm"]

    Then, build and run the container using docker-compose.yml:

    version: "3.7"
    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
          - "9000:9000"
        volumes:
          - .:/var/www/html
      nginx:
        image: nginx:alpine
        ports:
          - "80:80"
        volumes:
          - ./nginx.conf:/etc/nginx/conf.d/default.conf
        depends_on:
          - app

    Finally, create an nginx.conf

    server {
        listen 80;
        server_name localhost;
        root /var/www/html/public;
    
        index index.php index.html;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ .php$ {
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

    Run: docker-compose up -d

2. Manual Installation (Not Recommended):

  • Explanation: This method involves manually downloading and installing all the necessary dependencies (PHP, Nginx/Apache, MySQL/MariaDB, PHP extensions) and then manually configuring Laravel to work with them. This is a complex and error-prone process.

  • Advantages: Potentially more control over the installation process (though usually unnecessary).

  • Disadvantages: Time-consuming, complex, high risk of errors, difficult to maintain and update.

  • Why it’s not recommended: Modern package managers like Composer and containerization technologies like Docker provide much more efficient and reliable ways to manage dependencies and deployments. Manual installation is only advisable if you have very specific and unusual requirements that cannot be met by other methods. It is not efficient and can lead to many problems.

These alternative solutions offer different trade-offs in terms of complexity, speed, and control. While manual installation is generally discouraged, using a pre-built VM or Docker container can be a convenient option for quick deployments or testing environments. However, for most production environments, the Composer-based installation method outlined in the main article remains the most robust and maintainable approach.

Leave a Reply

Your email address will not be published. Required fields are marked *