Running laravel on a server
Running laravel in a server
Prerequisite
Server with ssh access and Ubuntu OS
Open port 80(HTTP) and 443(HTTPS)
domain name linked with the server or public IP address of the server
Installations
1. Update and upgrade
First of all, let’s update and upgrade the server
sudo apt update && sudo apt upgrade -y
2. Installing Nginx
We will be using Nginx for the webserver so let’s install it.
sudo apt install nginx -y
Once you install the nginx, you can verify the installation of nginx by using the command
nginx -v
#Output
:'
nginx version: nginx/1.20.1
'

Also, you can verify the service is running by using the command
sudo systemctl status nginx

If the Active: active is showing, you can now go to the IP address or domain of the server, and see the default welcome page of the nginx.

3. Installing php
We will be using php8.0. Currently, the latest version is PHP is 8.1 but we will be still installing 8.0
Preparing the system to install
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo add-apt-repository ppa:ondrej/nginx sudo apt updateInstalling php8.0
sudo apt install php8.0-fpmNote
Install the
php-fpmmodule, but do not installphp. If thephpmodule is installed first, it configures the Apache server instead of NGINX. Thephp-fpmpackage contains all core modules for PHP.You can verify that the PHP is installed in your system by
php -v # should have output like : 'PHP 8.0.15 (cli) (built: Jan 29 2022 07:24:52) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.15, Copyright (c) Zend Technologies with Zend OPcache v8.0.15, Copyright (c), by Zend Technologies '
Installing other php dependencies required for laravel
# here we are installing php8.0-mysql as we will be using MySQL as the database. you will need to install other package if you are using any other database sudo apt install php8.0-gd php8.0-mbstring php8.0-xml php8.0-bcmath php8.0-zip php8.0-curl php8.0-mcrypt php8.0-mysql -y
3. Running PHP
You have now PHP and nginx (webserver) in the system.
Let’s now change some configuration of nginx so that you can run .php file ie. serve the file from the server.
First, let’s create a
index.phpfile in/var/wwwfolder.cd /var/www echo '<?php echo "Laravel deployment in 5 mins";' | sudo tee index.php > /dev/nullIf you go to your domain/ip address ie.
[domain.name/index.php](http://domain.name/index.php)you will see 404 page rendered by nginx. As web server still doesn’t know how to serve phpNow, you have to update the configuration of nginx.
cd /etc/nginx/sites-available # open default file using nano or vim.# delete all the content of default and past the following configuration server { listen 80; server_name default_server; root /var/www; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; } location ~ /\.ht { deny all; } }After changing the configuration you can test if the configuration file is correct not by
sudo nginx -tIf the configuration file is current you should have output live above. If there are any issues fix the issues until you get the success output message.
Now, the configuration file test is a success, you have to reload the nginx to reflect the new configuration.
sudo systemctl restart nginxNow, when you go to your domain ie.
[domain.name/index.phpyou](http://domain.name/index.phpyou) will get the output as shown below.
4. Installing composer
The "Composer" is the package manager of PHP. I guess you are quite familiar with it. Let’s install it on the server.
EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --quiet
rm composer-setup.php
sudo mv ./composer.phar /usr/local/bin/composer
Now, check if the composer is installed on your server or not by
composer --version

5. Adding laravel project
For now, we will just install a new copy of the laravel project directly using Composer.
composer create-project laravel/laravel your-project-name #create new laravel proejct
sudo cp -r your-project-name /var/www #copy file to /var/www
You now have to update the nginx config file where you have to change the entry file of the project
- root /var/www;
+ root /var/www/your-project-name/public
Now you have to change some permission of files inside the laravel project to actually run the project smoothly.
sudo chmod -R 755 /var/www/your-project-name/storage /var/www/your-project-name/bootstrap/cache
sudo chown -R www-data:www-data /var/www/your-project-name/storage /var/www/your-project-name/bootstrap/cache
Now if you go to your domain name, you will be greeted with the laravel default home page.

6. Installing database
Today will we install MySQL and use it as our database.
sudo apt install mysql-server -y
you can check if MySQL is installed by
mysql --version
No, you can't run a predefined default script of MySQL to change the default settings of MySQL and configure the user.
sudo mysql_secure_installationFailed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
From 2022, will get this error. (insert digital ocen link here) https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04
7. Create a database, user, and password
we need a new database for the project. It’s wise to use a different user and password for the particular database which has access to only a given database.
sudo mysql
# you will be prompted to mysql console
create database laravel;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel_user'@'localhost';
exit
Now you should be able to log in to the MySQL using new database
mysql -u laravel_user -p
show databases # you will see laravel database

8. Updating a value in .env file of laravel to access database.
#/var/www/your-project-name/.env
- DB_DATABASE=laravel
- DB_USERNAME=root
- DB_PASSWORD=
+ DB_DATABASE=laravel
+ DB_USERNAME=laravel_user
+ DB_PASSWORD=password
now you can run the migration
php artisan migrate
php artisan db:seed
9. Adding authentication
We can add authentication to look if we have correctly added database to the project.
composer require laravel-ui
php artisan ui bootstrap --auth
then if you go to your server /register you will find the register form. Fill the form and click register, you will be registered and logged in. This shows your connection to dabase corretly
Congratulation, you have now deployed laravel.

