Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

How deploy Laravel on Ubuntu

Package needed :
apache2 or nginx webserver | php8.0 | mysql-server | zip | unzip |
ffmpeg | git | composer 2x | imagemagick | graphicsmagick |
libapache2-mod-php8.0

Credentials Needed :
Server SSH root access & Domain Access

First of all we need to login your server through ssh console using putty(for windows) or git
bash or terminal (Unix based system like mac, ubuntu etc..)

Now we need to run the command from our linux or windows terminal.
sudo apt-get update #For Repository Update

sudo apt-get -y upgrade #For Package Update

sudo apt-get dist-upgrade #For intelligently install or remove


packages as needed

sudo apt-get autoremove #For Remove Packages No Longer Needed

sudo apt-get autoclean #only removes files that can no longer be


downloaded and are virtually useless

sudo apt-get install -y apache2 #For Apache2 Server Installation

sudo service apache2 restart #Restart Apache

Php8.0-xm The Below Command for the php installation. Required php
package for laravel and composer

sudo apt-get -y install php8.0 php8.0-dba php8.0-gnupg php8.0-lz4


php8.0-oauth php8.0-raphf php8.0-swoole php8.0-xmlrpc php8.0-amqp
php8.0-decimal php8.0-grpc php8.0-mailparse php8.0-odbc php8.0-
readline php8.0-sybase php8.0-xsl php8.0-dev php8.0-http php8.0-
maxminddb php8.0-opcache php8.0-redis php8.0-tidy php8.0-ast php8.0-ds
php8.0-igbinary php8.0-mbstring php8.0-pcov php8.0-rrd php8.0-uopz
php8.0-yaml php8.0-bcmath php8.0-enchant php8.0-imagick php8.0-mcrypt
php8.0-pgsql php8.0-smbclient php8.0-uploadprogress php8.0-zip php8.0-
bz2 php8.0-fpm php8.0-imap php8.0-memcache php8.0-phpdbg php8.0-snmp
php8.0-uuid php8.0-zmq php8.0-cgi php8.0-gd php8.0-inotify php8.0-
memcached php8.0-protobuf php8.0-soap php8.0-vips php8.0-zstd php8.0-
cli php8.0-gearman php8.0-interbase php8.0-mongodb php8.0-ps php8.0-
solr php8.0-xdebug php8.0-common php8.0-gmagick php8.0-intl php8.0-
msgpack php8.0-pspell php8.0-sqlite3 php8.0-xhprof php8.0-curl php8.0-
gmp php8.0-ldap php8.0-mysql php8.0-psr php8.0-ssh2 php8.0-xml

sudo apt-get -y install mysql-server #For Mysql DataBase Server


Installation

Install composer from here https://getcomposer.org/download/ #For PHP


Dependency Management Tools

The below command used for


Zip/unzip : Archive Unarchive files
Ffmpeg : For Media converting tools
Imagemagick and Graphicsmagick : for php image optimization
Git and git-flow : For version control or source code repositary
Libapache2-mod-php8.0 : For enable php for apache2 server

sudo apt-get -y install zip unzip ffmpeg imagemagick graphicsmagick


git git-flow libapache2-mod-php8.0

sudo a2enmod actions fcgid alias proxy_fcgi rewrite headers #Need to


enable the following module for php and apache2 FCGI/PhpFPM interface

Need to Run This Command For Configure The php.ini Files for Avoiding
Another Server Issue.

sudo find /etc/php/8.0/ -name php.ini -exec sed -i


's/max_execution_time = 30/max_execution_time = 300/g' {} ';' -exec
sed -i 's/max_input_time = 60/max_input_time = 600/g' {} ';' -exec sed
-i 's/memory_limit = 128M/memory_limit = 1024M/g' {} ';' -exec sed -i
's/zlib.output_compression = Off/zlib.output_compression = On/g' {}
';'

sudo service apache2 restart #Restart Apache2 For Reflect The New
Changes
sudo service mysql restart #Restart Mysql Server

After restarting we need to upload/clone the source code in your webserver. For this process
you need to run the following command

mkdir -p /path/to/your/web/application #Used for create a directory in


your server

cd /path/to/your/web/application #Go/Change your directory

Then You need to Upload The Files In Your WebServer or You Can Clone The Code From Your
Git Repository.
For example:
cd /srv/www/html
git clone https://github.com/project/repository.git

Otherwise you can manually upload your files/source code using filezilla or another ftp/sftp
client.

After That We need to create DataBase And Apache2 Virtualhost for your web application using
following command
Mysql Part
sudo -i #For go to the root user

mysql -u root -p #And Hit enter without password for mysql server
login

CREATE DATABASE Your-DB-Name-Here; #For create a new database

#For Create a new database user with mysql native password for phpmyadmin login
CREATE USER 'Your-DB-User-Name-Here'@'localhost' IDENTIFIED WITH
mysql_native_password BY 'Enter-Your-Secret-Password@Here';
#For grant permission to user in your new database
grant all privileges on Your-DB-Name-Here.* to Your-DataBase-User-
Here@localhost;

Apache2 Virtualhost Part Domain/Subdomain


Here we need to create a virtualhost for your domain mapping in your webserver. So let’s start it
Just enter the following command in your system terminal
sudo -i #For go to the root user

cd /etc/apache2/sites-available/ #Go to Virtualhost Folder

vim filename-whatever-you-want.conf
#Then hit Enter and copy and paste the following code in your terminal

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName example.com
ServerAlias www.example.com
ServerAlias www.another-traffic-you-want-to-forward-to-this-apps.com
#OtherWise you can remove the another server alias record
ServerAdmin server@example.com.com
DocumentRoot /path/to/your/source/code/public

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,


# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
<Directory "/path/to/your/source/code/public">
DirectoryIndex index.php
AllowOverride all
Require all granted
# Options Indexes FollowSymLinks Includes ExecCGI
</Directory>
ErrorLog ${APACHE_LOG_DIR}/your-apps-name.error.log
CustomLog ${APACHE_LOG_DIR}/your-apps-name.log combined
<FilesMatch "\.php$">
SetHandler "proxy:fcgi://127.0.0.1:9004/"
</FilesMatch>
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Then Save it And Exit it using shift+zz key in your keyboard.

After that we need to enable the virtualhost using this command


Sudo a2ensite your-new-virtualHost-sfile-neme.conf

After that we need to configure the php-fpm for your web application for run php under another
user:
#ConFigure listen on a TCP socket to a specific IPv4 address on a specific port.

sudo sed '37i\listen = 127.0.0.1:9008'


/etc/php/8.0/fpm/pool.d/www.conf

#Change default user's group www-data to current logged users

sudo sed -i "s|www-data|ubuntu|g" /etc/php/8.0/fpm/pool.d/www.conf

After that we need to run the following command for reflect the changes:

sudo service apache2 restart ; sudo service php8.0-fpm restart

#create an info.php file for get php version info, extertion info etc...

sudo printf "<?php\nphpinfo();\n?>" > /var/www/html/info.php;

And now to to install the php dependency in your web application using this command
cd /path/to/your/web/application #Go to your project webroot
cp .env.example .env
vim .env

And replace the following details as per your configuration


APP_NAME="Apps Name"
APP_URL=https://example.com
DB_DATABASE=Database-Name-Here
DB_USERNAME=Database-User-Here
DB_PASSWORD=DataBase-Password-Here

MAIL_HOST=smtp.email.com
MAIL_PORT=587
MAIL_USERNAME=example@email.com
MAIL_PASSWORD=Your-Email_Password-Here
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=Your-email@example.com

Save IT and Exit It using shift+zz command

composer install
php artisan config:cache
php artisan migrate:refresh --seed
php artisan config:cache

Now need to change the file permission using following command.

chmod -R 777 storage


chown -R your-php-fpm-user:your-php-fpm-group

For example
sudo chown -R ubuntu:ubuntu .

Everything is done and ust hits your application url in your browser and see the result.

And Thank You Very Much For Reading This Tutorial, If Anything Else In
There Then Don’t Heist Your Time To Ask Me.

You might also like