How to Install Laravel on CentOS 8

How to Install Laravel on CentOS 8

How to Install Laravel on CentOS 8

Laravel is a powerful PHP framework used for web development. If you're using CentOS 8, this guide will help you install and set up Laravel step by step.

What You’ll Learn:
✔️ Install PHP, Composer, and Laravel
✔️ Set up a web server (Apache or Nginx)
✔️ Configure Laravel and database
✔️ Run Laravel on CentOS 8

Step 1: Update Your System

Before installing Laravel, update your system:

sudo dnf update -y

Step 2: Install PHP and Required Extensions

Laravel requires PHP 8.1+. Enable the Remi repository and install PHP:

sudo dnf install epel-release -y sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y sudo dnf module reset php sudo dnf module enable php:remi-8.1 -y sudo dnf install php php-cli php-mbstring php-xml php-bcmath php-curl php-zip unzip curl -y

Verify the PHP installation:

php -v

Step 3: Install Composer

Laravel uses Composer for dependency management. Install it using:

curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer

Check if Composer is installed:

composer -V

Step 4: Install MySQL and Configure the Database

If MySQL is not installed, install it using:

sudo dnf install mysql-server -y

Start and enable MySQL:

sudo systemctl start mysqld sudo systemctl enable mysqld

Secure your MySQL installation:

sudo mysql_secure_installation

Log in to MySQL and create a database for Laravel:

sudo mysql -u root -p

Run the following SQL commands:

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;

Step 5: Install Laravel

Navigate to your web directory:

cd /var/www

Use Composer to install Laravel:

sudo composer create-project laravel/laravel myapp

Set proper ownership and permissions:

sudo chown -R apache:apache /var/www/myapp sudo chmod -R 775 /var/www/myapp/storage /var/www/myapp/bootstrap/cache

Step 6: Configure Apache or Nginx for Laravel

For Apache:

Enable required modules:

sudo dnf install httpd -y sudo systemctl start httpd sudo systemctl enable httpd sudo firewall-cmd --add-service=http --permanent sudo firewall-cmd --reload

Create a new virtual host file:

sudo nano /etc/httpd/conf.d/laravel.conf

Paste the following configuration:

<VirtualHost *:80> ServerName yourdomain.com DocumentRoot /var/www/myapp/public <Directory /var/www/myapp> AllowOverride All Require all granted </Directory> ErrorLog /var/log/httpd/laravel-error.log CustomLog /var/log/httpd/laravel-access.log combined </VirtualHost>

Restart Apache:

sudo systemctl restart httpd

For Nginx:

Install Nginx and PHP-FPM:

sudo dnf install nginx php-fpm -y sudo systemctl start nginx sudo systemctl enable nginx

Create a new Nginx configuration:

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

Paste the following configuration:

server { listen 80; server_name yourdomain.com; root /var/www/myapp/public; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /\.ht { deny all; } }

Restart Nginx:

sudo systemctl restart nginx

Step 7: Configure Laravel Environment

Navigate to your Laravel project directory:

cd /var/www/myapp

Edit the .env file to update database credentials:

nano .env

Modify these lines:

DB_DATABASE=laravel_db DB_USERNAME=laravel_user DB_PASSWORD=your_password

Run database migrations:

php artisan migrate

Step 8: Run Laravel

Start the Laravel development server:

php artisan serve --host=0.0.0.0 --port=8000

Now, open your browser and visit:

http://your-server-ip:8000

Step 9: Optimize Laravel Performance

Run these optimization commands:

php artisan config:cache php artisan route:cache php artisan view:cache

Set correct permissions:

sudo chmod -R 775 storage bootstrap/cache sudo chown -R apache:apache storage bootstrap/cache

Conclusion

🎉 Congratulations! You have successfully installed Laravel on CentOS 8. Now, you can start building amazing web applications! 🚀

💬 Have any questions? Let us know in the comments!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close