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
-
Update and Upgrade Packages:
sudo apt update sudo apt upgrade -y
-
Install Essential Packages:
sudo apt install -y software-properties-common
2. Install Apache
-
Install Apache:
sudo apt install -y apache2
-
Enable Apache to Start on Boot and Start Apache:
sudo systemctl enable apache2 sudo systemctl start apache2
-
Adjust Firewall to Allow Web Traffic:
sudo ufw allow in "Apache Full"
3. Install MySQL
-
Install MySQL Server:
sudo apt install -y mysql-server
-
Secure MySQL Installation:
sudo mysql_secure_installation
Follow the prompts to set a root password and secure your installation.
-
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
-
Add PHP Repository:
sudo add-apt-repository ppa:ondrej/php sudo apt update
-
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
-
Configure Apache to Use PHP:
sudo a2enmod php sudo systemctl restart apache2
5. Install Composer
- 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
-
Install Laravel Installer via Composer:
composer global require laravel/installer
-
Add Composer's Global Bin Directory to Your PATH:
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc source ~/.bashrc
-
Create a New Laravel Project:
cd /var/www composer create-project --prefer-dist laravel/laravel your_project_name
7. Configure Apache for Laravel
-
Create a New Apache Configuration File for Your Laravel Project:
sudo nano /etc/apache2/sites-available/your_project_name.conf
-
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>
-
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
-
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
-
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. -
Generate Application Key:
php artisan key:generate
9. Finalize the Setup
-
Migrate the Database:
php artisan migrate
-
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.
Sami Rahimi
Innovate relentlessly. Shape the future..
Recent Comments