Introduction:
Laravel is a popular PHP web application framework known for its elegant syntax and developer-friendly features. CentOS 8 is a robust and secure Linux distribution widely used for server deployments. In this article, we’ll walk you through the step-by-step process of installing Laravel on a CentOS 8 server. By the end, you’ll have a fully functional Laravel environment ready for web development.
Prerequisites:
Before we begin, make sure you have the following prerequisites:
- A CentOS 8 server with root or sudo access.
- A basic understanding of Linux commands.
Step 1: Update Your System
Start by updating your system’s package list to ensure you have the latest software packages:
sudo yum update
Step 2: Install LAMP Stack
Laravel requires a web server, PHP, and a database. We’ll set up a LAMP (Linux, Apache, MySQL, PHP) stack. Run the following commands to install the necessary components:
sudo yum install httpd mariadb mariadb-server php php-cli php-mysqlnd
Step 3: Start and Enable Services
Now that the packages are installed, start the Apache and MariaDB services and enable them to start on boot:
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl start mariadb
sudo systemctl enable mariadb
Step 4: Secure Your MariaDB Installation
Run the following command to secure your MariaDB installation and set a root password:
sudo mysql_secure_installation
Follow the on-screen prompts to configure MariaDB security settings.
Step 5: Create a Database
Log in to the MariaDB server using the root account:
sudo mysql -u root -p
Enter the root password when prompted. Then, create a database for your Laravel application:
CREATE DATABASE laravel;
Create a dedicated user and grant privileges to the database:
CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel.* TO 'laraveluser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 6: Install Composer
Composer is a dependency management tool for PHP that Laravel uses. Install Composer globally:
sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Step 7: Install Laravel
Navigate to your preferred web directory. Typically, this is /var/www/html/
:
cd /var/www/html/
Install Laravel using Composer:
sudo composer create-project --prefer-dist laravel/laravel your_project_name
Replace your_project_name
with your desired project name.
Step 8: Configure Laravel
Create a .env
file by copying the example file:
cp .env.example .env
Generate an application key:
php artisan key:generate
Step 9: Configure Apache
Create a new Apache configuration file for your Laravel project. Create a file named your_project_name.conf
in the /etc/httpd/conf.d/
directory and add the following:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/your_project_name/public
ServerName your_domain_or_server_ip
<Directory /var/www/html/your_project_name>
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/your_project_name_error.log
CustomLog /var/log/httpd/your_project_name_access.log combined
</VirtualHost>
Replace your_domain_or_server_ip
with your server’s domain name or IP address.
Step 10: Enable the Virtual Host
Enable the virtual host and restart Apache:
sudo systemctl restart httpd
Step 11: Set Permissions
Ensure that the web server has the necessary permissions to write to storage and cache directories:
sudo chown -R apache:apache /var/www/html/your_project_name/storage
sudo chown -R apache:apache /var/www/html/your_project_name/bootstrap/cache
Step 12: Access Your Laravel Application
You should now be able to access your Laravel application by navigating to your server’s IP address or domain name in your web browser.
Conclusion:
Congratulations! You’ve successfully installed Laravel on your CentOS 8 Linux server. You can now start building and deploying web applications using the power and flexibility of Laravel. Remember to keep your server and Laravel installation secure by regularly applying updates and following best practices for web application development and server administration.