-
Ubuntu Server
- It can be obtained from any hosting provider such as Digital Ocean, AWS, Azure, Google Cloud, Vultr, Linode, Interserver, Contabo.
-
Git repository of a Laravel application
-
Domain
- Register and buy a new domain for the Laravel application through Namecheap.
- It's a combination of four technologies which include Linux, Enginx, MySql and Php. This stack can be used to host web apps built using php frameworks such as Laravel.
-
Open the terminal and login as the root in the server:
ssh root@ipaddress
-
Create a new user:
sudo adduser user
-
Add the new user in sudo visudo:
sudo visudo
# User privilege specification root ALL=(ALL:ALL) ALL
-
Logout and login as the new user:
ssh newuser@ipaddress
NOTE:
newuser
is the new user you created andipaddress
is the ipaddress of your server.Avoid running commands using root
for the next steps.
-
Ensure the server is upto date before installing any packages.
sudo apt-get update
-
Install nginx in your server:
sudo apt-get install nginx
-
Confirm if it has installed by checking
http://server-ipaddress
on the browser. The nginx welcome page will be displayed.
-
Install the MySql server where the laravel app database will be stored.
sudo apt-get install mysql-server
-
Secure the database server in production:
sudo mysql_secure_installation
Configurations:
VALIDATE PASSWORD: N Change existing password for root: N Removing anonymous users: Y Disallow root login remotely: Y Remove the test database: Y Reload the privileges table: Y
-
Plugins to install:
-
php-fpm
: Stands for FastCGI Process Manager. Used for processing. -
php-mysql
: Allows one to query MySQL using Eloquent. -
php-mbstring
: Laravel requirement.
sudo add-apt-repository -y ppa:ondrej/php sudo apt-get update sudo apt-get install php-fpm php-mysql php-mbstring unzip php-json php-bcmath php-zip php-gd php-tokenizer php-xml
NOTE:
If you get the errorapt add repository command not found
, runsudo apt-get install software-properties-common
. Afterwards try and install PHP again. -
-
Confirm the PHP version installed:
php -v
-
Composer: PHP dependency manager that will keep track of libraries and dependencies that PHP applications need. Install it:
curl -sS https://getcomposer.org/installer | php
-
Move the downloaded binary to the system directory:
sudo mv composer.phar /usr/local/bin/composer
-
Confirm the composer version installed:
composer --version
-
Install the
nano
editor:sudo apt-get install nano
-
Modify
php.ini
file:sudo nano /etc/php/8.2/fpm/php.ini
NOTE:
Replace8.2
with the php version installed.- Press
ctrl
+w
and search forcgi.fix_pathinfo=
. Uncomment the line and replace1
to0
. - Press
ctrl
+x
and enterY
to save the file. Press the enter key to exit nano.
- Press
-
Restart
php-fpm
for the changes to take place:sudo systemctl restart php8.2-fpm
-
Open the nginx configuration file:
sudo nano /etc/nginx/sites-available/default
-
Modify the file to look like:
server { listen 80 default_server; listen [::]:80 default_server; root /var/www/laravel/project-folder/public; index index.php index.html index.htm index.nginx-debian.html; server_name server-ipaddress; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; } location ~ /\.ht { deny all; } }
NOTE:
Replace root /var/www/laravel/project-folder
/public as per the name of your project folder name andserver-ipaddress
.
-
-
Confirm the configuration file has no errors:
sudo nginx -t
-
Restart nginx:
sudo systemctl reload nginx
-
Login to the MySql server:
mysql -u root -p
- Enter the user password when prompted.
-
Create the project database:
CREATE DATABASE project-database;
-
Assign the privileges to the user handling the database:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
-
Flush the privileges for the changes to take place:
FLUSH PRIVILEGES;
-
Check if the database has been created:
SHOW DATABASES;
-
Create the directory that will host the Laravel application:
sudo mkdir /var/www/laravel && cd /var/www/laravel
-
Clone the project from GitHub:
git clone https://github.com/username/project.git
-
Run Composer:
composer install --no-dev
-
Change the ownership of the project to the web group:
sudo chown -R user:www-data /var/www/laravel/project
-
Assigning read-write permissions to the storage folder:
sudo chmod -R 775 /var/www/laravel/project/storage
-
Change directory to the cloned project folder using
cd /var/www/laravel/project
-
Generate the encryption key for the app:
php artisan key:generate
-
Update the credentials in the .env file:
sudo nano .env
Configurations:
APP_ENV=production APP_DEBUG=false APP_KEY=base64:kGKg6DnMqMGBJrLGDh4Jg+bTIXqcVXZKqJdqueTlkCk= APP_URL=http://mydomain.com DB_HOST=localhost DB_DATABASE=yourdatabasename DB_USERNAME='root' DB_PASSWORD='yourdatabasepassword' CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync REDIS_HOST=localhost REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=googlemail.com MAIL_PORT=465 MAIL_USERNAME=XXXXXXXXXXX MAIL_PASSWORD=XXXXXXXXXXX MAIL_ENCRYPTION=null
NOTE:
ReplaceAPP_URL
to the registered domain mentioned in the prerequisites,DB_DATABASE
to the name of the project database,DB_USERNAME
to the user handling the project database andDB_PASSWORD
to the mysql user password. Don't change yourAPP_KEY
configuration. -
Create a symlink to the public folder to display files:
sudo php artisan storage:link
-
Cache necessary configurations to make the app faster:
sudo php artisan optimize
-
Migrate the database:
sudo php artisan migrate --force
-
Seed the database:
sudo php artisan db:seed
-
Acces the Laravel application on your browser using
http://server-ipaddress
orhttp://www.project.com
.
-
Install Certbot client:
sudo apt-get install certbot python3-certbot-nginx -y
-
Request the SSL certificate for the registered domain:
sudo certbot --nginx -d www.project.com
-
Access the app using
https://www.project.com
.