CentOS 7 is a widely used and stable Linux distribution favored by many developers and system administrators. If you’re looking to set up a Laravel development environment on CentOS 7, you’ve come to the right place. Laravel, a powerful PHP framework, can be the foundation of your web application dreams. In this article, I’ll leverage my over a decade of experience as a SaaS content writer to provide a step-by-step guide on how to install Laravel on CentOS 7.
Prerequisites
Before we dive into the installation process, let’s ensure you have all the prerequisites in place:
- CentOS 7 Server: You need a CentOS 7 server or a virtual machine to host your Laravel application.
- SSH Access: Ensure you can access your server via SSH for remote administration.
- Root or Sudo Privileges: You’ll need root or sudo privileges to execute system-level commands.
- Basic Command Line Skills: Familiarize yourself with basic command-line operations.
Setting Up a CentOS 7 Server
If you haven’t already set up your CentOS 7 server, here’s a brief overview:
- Download the CentOS 7 ISO image from the official website.
- Install CentOS 7 on your server or virtual machine following the installation prompts.
- Make sure you have a network connection to your CentOS 7 server.
- After the installation, log in with the user you created during the setup or as the root user.
Installing PHP and Composer
Laravel relies on PHP as its core programming language. CentOS 7’s default repositories provide PHP 5.4, which is outdated. To work with Laravel, we need PHP 7.2 or higher. We’ll also install Composer, a PHP dependency manager.
To update PHP and install Composer, follow these steps:
Step 1: Enable the EPEL repository
EPEL (Extra Packages for Enterprise Linux) provides additional packages for CentOS. Use the following command to enable the EPEL repository:
sudo yum install epel-release
Step 2: Install PHP 7.4 and Composer
Run the following commands to install PHP 7.4 and Composer:
sudo yum install php php-cli php-fpm php-json php-common php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
Now, you have PHP 7.4 and essential PHP extensions. Let’s proceed to install Composer.
sudo yum install composer
Composer is a vital tool for Laravel as it manages the project’s dependencies efficiently.
Configuring a Database
Laravel supports multiple database systems, but for this guide, we’ll use MySQL, a popular choice. To install MySQL, execute the following commands:
sudo yum install mysql-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
Now, let’s secure your MySQL installation:
sudo mysql_secure_installation
You’ll be prompted to set a root password, remove anonymous users, and more. Follow the on-screen prompts to complete the setup.
Installing Laravel
With PHP, Composer, and MySQL in place, it’s time to install Laravel. Follow these steps:
Step 1: Create a Laravel Project
Navigate to the directory where you want to create your Laravel project. For instance, if you want to create a project named “mylaravelapp,” use the following command:
composer create-project --prefer-dist laravel/laravel mylaravelapp
Composer will download Laravel and its dependencies.
Step 2: Configure the Environment
Move to the project directory:
cd mylaravelapp
Rename the .env.example
file to .env
:
mv .env.example .env
Generate a unique application key:
php artisan key:generate
Your Laravel application is now configured and ready.
Setting Up Virtual Hosts
To serve your Laravel application using a web server like Apache or Nginx, you need to set up virtual hosts. In this example, we’ll use Apache.
Step 1: Install Apache
If you haven’t installed Apache yet, you can do so with the following command:
sudo yum install httpd
Step 2: Configure Virtual Host
Create a new Apache configuration file for your Laravel application:
sudo nano /etc/httpd/conf.d/mylaravelapp.conf
Add the following configuration, replacing /var/www/html/mylaravelapp/public
with the actual path to your Laravel project’s public
directory:
<VirtualHost *:80>
ServerAdmin webmaster@mylaravelapp
DocumentRoot /var/www/html/mylaravelapp/public
ServerName mylaravelapp
<Directory /var/www/html/mylaravelapp>Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/mylaravelapp-error.logCustomLog /var/log/httpd/mylaravelapp-access.log common
</VirtualHost>
Save the file and exit the text editor.
Step 3: Enable and Start Apache
Enable Apache to start at boot:
sudo systemctl enable httpd
Start the Apache service:
sudo systemctl start httpd
Testing Laravel Installation
With your Laravel project and web server set up, it’s time to test your installation. Open a web browser and navigate to your server’s IP address or domain name. You should see the Laravel welcome page, confirming a successful installation.
Related Articles:
Conclusion
You’ve successfully installed Laravel on CentOS 7, a robust combination for building web applications. From setting up your CentOS server to configuring PHP, Composer, and MySQL, this guide has covered the essentials. You’ve also learned to create a Laravel project, set up virtual hosts, and test your installation.
Laravel’s elegant syntax and powerful features make it a top choice for web development. With the right environment and knowledge, you can harness its capabilities to build remarkable web applications. Happy coding!