Deploy Laravel project on Server

Introduction

This article is mainly about how to deploy Laravel on AWS EC2
Here are some points:

  1. LAMP deployment
  2. Composer deployment
  3. Laravel deployment

Specification

  • Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type

LAMP

PHP

  • PHP Installation

    sudo yum install php72;php -v
  • Install PHP Extension

    sudo yum install php72-mbstring
sudo yum install php72-bcmath
sudo yum install php72-pdo
sudo yum install php72-mysqlnd
sudo yum install php72-gd.x86_64

Apache

  • Install Apache

    sudo yum install httpd24
  • Launch Apache

    sudo service httpd start
  • Set up automatic start after reboot

    sudo chkconfig httpd on
  • Check if httpd is enabled

    chkconfig --list httpd
  • Set AWS security inbound

  • Test Apache by visiting the IP

  • Install SSL module

    sudo yum install mod24_ssl

MySQL

  • Install MySQL

    sudo yum install mysql57-server
  • Launch MySQL

    sudo service mysqld start
  • Set up MySQL to automatic start after reboot

    sudo chkconfig mysqld on
  • 執行 mysql_secure_installation

    sudo mysql_secure_installation
  • Set up MySQL to support remote connection with Sequel Pro

    CREATE USER 'root'@'%' IDENTIFIED BY '';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    FLUSH PRIVILEGES;

Set up authority

  • Add your user (in this case, ec2-user) to the apache group.
sudo usermod -a -G apache ec2-user
  • Log out and then log back in again to pick up the new group, and then verify your membership.
  1. Log out (use the exit command or close the terminal window):
exit
  1. To verify your membership in the apache group, reconnect to your instance, and then run the following command:
groups
  • Change the group ownership of /var/www and its contents to the apache group.
sudo chown -R ec2-user:apache /var/www
  • To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories.
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
  • To add group write permissions, recursively change the file permissions of /var/www and its subdirectories:
find /var/www -type f -exec sudo chmod 0664 {} \;

Test LAMP Web server

  • Create a PHP file in the Apache document root.
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
  • In a web browser, type the URL of the file that you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example:
    http://my.public.dns.amazonaws.com/phpinfo.php
  • Delete the phpinfo.php file. Although this can be useful information, it should not be broadcast to the internet for security reasons.
rm /var/www/html/phpinfo.php

Composer

  • Install Composer

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"
  • Make composer global

    sudo mv composer.phar /usr/local/bin/composer

Git

  • Install Git
    sudo yum install -y git

Deploy project

  • Clone project

    git clone repositoryAddress
  • Deploy .env file

    cp .env.example .env
  • Install Composer

    composer install
  • Generate key

    php artisan key:generate
  • Create database

    mysql -uroot
    create database databaseName;
  • Create tables

    php artisan migrate
  • If it lacks memory, you could assign some hard-drive to memory.

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2000;
    sudo chmod 600 /swapfile;
    sudo mkswap /swapfile;
    sudo swapon /swapfile;
    swapon -s;
    sudo vim /etc/fstab;

Add the following code

/swapfile swap swap defaults 0 0

  • Change teh default document root location
    sudo vim /etc/httpd/conf/httpd.conf
<Direction "/var/www/html">
Allow Override All
</Direction>



The above is normal process. You could also refer to lazy version as follows:

Lazy version

sudo yum install -y php72;
sudo yum install -y php72-mbstring;
sudo yum install -y php72-bcmath;
sudo yum install -y php72-pdo;
sudo yum install php72-gd.x86_64
sudo yum install -y php72-mysqlnd;
sudo yum install -y httpd24;
sudo service httpd start;
sudo chkconfig httpd on;
sudo yum install -y mod24_ssl;
sudo yum install -y mysql57-server;
sudo service mysqld start;
sudo chkconfig mysqld on;
sudo usermod -a -G apache ec2-user;
sudo chown -R ec2-user:apache /var/www;
sudo chmod 2775 /var/www
find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php;
php -r "unlink('composer-setup.php');";
sudo mv composer.phar /usr/local/bin/composer;
sudo yum install -y git;
sudo php -v
  • Set AWS security inbound

  • Test Apache by visiting the IP

  • Execute mysql_secure_installation

    sudo mysql_secure_installation
  • Set up MySQL to support Sequel Pro remote connection

    CREATE USER 'root'@'%' IDENTIFIED BY 'yourPassword';GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'  WITH GRANT OPTION;FLUSH PRIVILEGES;
  • Log out and then log back in again to pick up the new group, and then verify your membership.

  1. Log out (use the exit command or close the terminal window):
exit;
  1. To verify your membership in the apache group, reconnect to your instance, and then run the following command:
groups
  • Create a PHP file in the Apache document root.
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
  • In a web browser, type the URL of the file that you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example:
    http://my.public.dns.amazonaws.com/phpinfo.php
  • Delete the phpinfo.php file. Although this can be useful information, it should not be broadcast to the internet for security reasons.
rm /var/www/html/phpinfo.php
  • Clone project

    git clone repositoryAddress
  • Deploy .env file

    cp .env.example .env
  • Install Composer

    composer install
  • Generate key

    php artisan key:generate
  • Create database

    mysql -uroot
    create database databaseName;
  • Create tables

    php artisan migrate
  • If it lacks memory, you could assign some hard-drive to memory.

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2000;
    sudo chmod 600 /swapfile;
    sudo mkswap /swapfile;
    sudo swapon /swapfile;
    swapon -s;
    sudo vim /etc/fstab;

Add the following code

/swapfile swap swap defaults 0 0

  • Change teh default document root location
    sudo vim /etc/httpd/conf/httpd.conf
<Direction "/var/www/html">
Allow Override All
</Direction>
  • Restart Apache
    sudo service httpd restart

Check your OS version in Linux

  • Use lsb_release , If it shows command not found, install it

    sudo apt-get install lsb-release
  • Get its usage

    lsb_release --help

Example output:

-h, --help         show this help message and exit
-v, --version show LSB modules this system supports
-i, --id show distributor ID
-d, --description show description of this distribution
-r, --release show release number of this distribution
-c, --codename show code name of this distribution
-a, --all show all of the above information
-s, --short show requested information in short format

  • Per the information above, if you would like to check the detail:

    lsb_release -a
  • If you just want to know the version of the Kernel:

    uname -r
Kubernetes Engine - Qwik Start Use `Laravel` `template` and `blade`

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×