How to deploy Laravel on CentOS 7

Overview

I will introduce the procedure to deploy a Laravel project on a virtual server with CentOS on an EC2 instance of AWS.

Prerequisites

-Deploy a Laravel project that runs in the development environment and is managed by Git to CentOS 7. -This time, I will introduce only the contents to work by connecting to CentOS with ssh. -DB connects to Amazon RDS.

Work content

  1. Middleware installation (Apache, mysql-community-client, PHP7.3)
  2. Change OS settings (SELinux stop, time zone, locale)
  3. RDS connection, DB creation, setting
  4. Composer installation
  5. Source code Clone
  6. Set to work (Apache, Laravel)

1. Middleware installation (Apache, mysql-community-client, PHP7.3)

SSH from your PC to the server.

% ssh -i ~/.ssh/xxxxx.pem [email protected]

1.1 Change to root, check packages to update, update all packages, restart

Change to root

If you do not have root privileges, you may not be able to update the package due to insufficient privileges, so change to root.

$ sudo -i

Check for packages to be updated

CentOS installed from the media may contain old packages, so update all packages in advance. You can check the package to be updated this time with the following command.

[root]
$ yum check-update

Update all packages

[root]
$ yum update

Reboot

Some processes need to be restarted, so restart them.

[root]
$reboot

1.2 Add repository (epel, remi)

Add epel repository

[root]
$yum install epel-release.noarch

Add remi repository

[root]
$rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

1.3 Apache installation, startup, autostart settings

Apache installation

[root]
$yum install httpd

Apache startup

[root]
$ systemctl start httpd.service

Apache autostart settings

[root]
$systemctl enable httpd.service

Apache status check

It is OK if "enabled" is displayed and "Active: active (running)" is displayed in () of Loaded.

[root]
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running)since gold 2020-11-06 04:57:30 UTC; 42s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 8618 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─8618 /usr/sbin/httpd -DFOREGROUND
           ├─8619 /usr/sbin/httpd -DFOREGROUND
           ├─8620 /usr/sbin/httpd -DFOREGROUND
           ├─8621 /usr/sbin/httpd -DFOREGROUND
           ├─8622 /usr/sbin/httpd -DFOREGROUND
           └─8623 /usr/sbin/httpd -DFOREGROUND

1.4 Add MYSQL repository, install mysql-community-client, check connection

Add MYSQL repository

[root]
$yum install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

Install mysql-community-client

This time, I just need to be able to connect to Mysql in Amazon RDS, so it's OK if I can execute the Mysql command. Therefore, install only "mysql-community-client". If you want to create a DB in the server, please install "mysql-community-server".

Make sure the version you want is enabled (5.7)

Verify that mysql-community-client is available using the yum info command

[root]
$yum info mysql-community-client
Loaded plugin:extras_suggestions, langpacks, priorities, update-motd
37 packages excluded due to repository priority protections
Installed packages
name: mysql-community-client
architecture: x86_64
version: 5.7.32
release: 1.el7
capacity: 101 M
Repository: installed
Source repository: mysql57-community
wrap up: MySQL database client applications and tools
URL                 : http://www.mysql.com/
....

Make sure the mysql repository is enabled with the yum repolist command.

[root]
$yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 175
mysql-tools-community/x86_64      MySQL Tools Community                      120
mysql57-community/x86_64          MySQL 5.7 Community Server                 464

Install mysql-community-client

[root]
$yum install  mysql-community-client.x86_64

Connection confirmation

Connect to RDS. It's OK if you can confirm that you can connect to mysql.

[root]
$ mysql -u [DB user] -p -h [end point] -P 3306
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 322
Server version: 5.7.31-log Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

1.5 Installing PHP 7.3

Install php using remi-php73

[root]
$yum install php php-cli php-common php-devel php-gd php-mbstring php-mysqlnd php-pecl-mysql php-pdo php-xml php-pecl-memcache --enablerepo=remi-php73

$yum install --enablerepo=remi-php73 php-zip

Confirm that PHP version is 7.3 series

[root]
$php -v
PHP 7.3.24 (cli) (built: Oct 27 2020 11:01:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

Now you have an environment where Apache, Mysql, and PHP can work.

1.6 Git, zip, unzip, postfix installation

Add the required repositories here as appropriate.

[root]
$yum install git
$yum install zip unzip
$yum install postfix

2. Change OS settings (SELinux stop, time zone, locale)

2.1 SELinux outage

SELinux is one of the kernel control functions and is an abbreviation for Security-Enhanced-Linux. Fine access control is possible by using SELinux. However, it is difficult to manage because it is difficult and it behaves excessively, so disable it.

Checking SELinux status (enabled if Enforcing)

[root]
$ getenforce
Enforcing

Disable (disable SELINUX value)

[root]
$vi /etc/selinux/config
SELINUX=disabled

Reboot

[root]
$ reboot

Make sure it is disabled

$ getenforce
Disabled

2.2 Time zone change

In the default state, the time setting is "UTC, +0000". Change this to Japan time.

$timedatectl status
      Local time:Sat 2020-11-07 05:31:41 UTC
  Universal time:Sat 2020-11-07 05:31:41 UTC
        RTC time:Sat 2020-11-07 05:31:41
       Time zone: n/a (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

Set Asia / Tokyo in time zone

[root]
$timedatectl set-timezone Asia/Tokyo

Check if the tom zone has changed

It is OK if it is "Time zone: Asia / Tokyo (JST, +0900)".

[root]
$  timedatectl status
      Local time:Fri 2020-11-06 14:22:07 JST
  Universal time:Fri 2020-11-06 05:22:07 UTC
        RTC time:Fri 2020-11-06 05:22:07
       Time zone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

2.3 Locale change

Check current locale settings

[root]
$localectl status
   System Locale: LANG=en_US.UTF-8
       VC Keymap: us
      X11 Layout: us

Locale change

[root]
$localectl set-locale LANG=ja_JP.UTF-8

Set to Japanese 106 keyboard

[root]
$localectl set-keymap jp106

Confirm the change of locale

[root]
$localectl status
localectl status
   System Locale: LANG=ja_JP.UTF-8
       VC Keymap: jp106
      X11 Layout: jp
       X11 Model: jp106
     X11 Options: terminate:ctrl_alt_bksp

3. RDS connection, DB creation, application user creation

Now that you can connect to MYSQL in 1.4, it's time to create a DB and a database user.

Connect to RDS

[root]
$mysql -u [DB user] -p -h [end point] -P 3306

DB creation

mysql> create database [DB name];
Query OK, 1 row affected (0.00 sec)

Creating a database user

mysql>GRANT USAGE ON [DB name].* to [New DB user]@"%" IDENTIFIED BY '[password]';
Query OK, 0 rows affected, 1 warning (0.00 sec)

Grant user access to the database

mysql>GRANT ALL PRIVILEGES ON [DB name].* TO [New DB user]@"%";
Query OK, 0 rows affected (0.00 sec)

User permission confirmation

It is OK if it looks like the following.

mysql>show  grants for [New DB user]@%
+---------------------------------------------------------+
| Grants for [New DB user]@%                                    |
+---------------------------------------------------------+
| GRANT USAGE ON *.* TO '[New DB user]'@'%'                     |
| GRANT ALL PRIVILEGES ON `[DB name]`.* TO '[New DB user]'@'%' |
+---------------------------------------------------------+
2 rows in set (0.00 sec)

[New DB user] Check if the user can connect

Go back to EC2 and check if you can log in with the user you created this time.

[root]
$mysql -u [New DB user] -p -h [end point] -P 3306

4. composer installation

Download composer-setup.php by running copy with the php command

$php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

"Composer-setup.php" will be copied.

$ll
Total 272
-rw-rw-r--1 centos centos 276190 November 6 16:55 composer-setup.php

Execute the downloaded composer-setup.php to create an executable file (phar) of Composer

$php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 2.0.4) successfully installed to: /home/centos/composer.phar
Use it: php composer.phar

"Composer.phar" has been added to / home / centos.

Remove the installer

$php -r "unlink('composer-setup.php');"
$ ll
2132 in total
-rwxr-xr-x 1 centos centos 2180422 November 6 16:56 composer.phar

Moved for global use

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

Make sure composer is working

$ composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.4 2020-10-30 22:39:11
.......

5. Clone the Laravel source code

Move to the directory to place

This time, we will place the project to Laravel project directly under / var / www.

$ cd /var/www/

Clone source

$git clone remote repository.git
fatal: could not create work tree dir 'xxxx'.: ????????

An error occurred because you do not have permission for the directory to clone.

Change permissions

The owner and owning group of "www" was "root", so change it to "centos".

[root]
$ ll
drwxr-xr-x.4 root root 33 November 6 13:56 www

$ chown centos:centos www
$ ll
drwxr-xr-x.4 centos centos 33 November 6 13:56 www

Clone the source again.

$git clone remote repository.git
remote: Enumerating objects: 2841, done.
remote: Counting objects: 100% (2841/2841), done.
remote: Compressing objects: 100% (2767/2767), done.
remote: Total 2841 (delta 1874), reused 110 (delta 22)
Receiving objects: 100% (2841/2841), 33.21 MiB | 35.94 MiB/s, done.
Resolving deltas: 100% (1874/1874), done.

$ ll
4 in total
drwxrwxr-x 13 centos centos 4096 November 6 17:15 xxxx(cloned source)
drwxr-xr-x.2 root root 6 April 2 2020 cgi-bin
drwxr-xr-x.2 root root 6 April 2 2020 html

The clone was successful. However, if you look inside the directory, you will find the "cgi-bin" and "html" directories. I will not use it this time, so I will delete it.

Remove cgi-bin and html

[root]
$ rm -r html
rm:directory`html'Do you want to delete? yes
$ rm -r cgi-bin
rm:directory`cgi-bin'Do you want to delete? yes
$ ll
4 in total
drwxrwxr-x 13 centos centos 4096 November 6 17:15 xxxx(cloned source)

5. Set to work (Apache, Laravel)

5.1 Apache settings

Add the following to httpd.conf

Add the following so that "ServerName" and "DocumentRoot" .htaccess will work.

$vi /etc/httpd/conf/httpd.conf

ServerName xxxx.com:80
DocumentRoot "/var/www/xxxx/public"

<Directory /var/www/xxxx/public>
    AllowOverride All
</Directory>

Apache restart

Reboot for the settings to take effect.

[root]
$ systemctl restart httpd

5.2 Laravel settings

From here, change the settings so that Laravel works. composer install

cd /var/www/xxxx
$ composer install

Clear cache for config and route

$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

$ php artisan route:cache
Route cache cleared!

Change permissions

$ chmod 777 storage -R
$ chmod 777 bootstrap -R

Confirmation

Make sure the permissions have changed.

$ ll
drwxrwxrwx 5 centos centos 46 November 6 17:15 storage
drwxrwxrwx 3 centos centos 34 November 6 17:15 bootstrap

.env settings

Next, edit ".env" that sets the environment of Laravel.

$cp .env.example .env
$vi .env

APP_ENV=production
APP_URL=xxxx.com
APP_DEBUG=false

DB_HOST=[end point]
DB_PORT=3306
DB_DATABASE=[DB name]
DB_USERNAME=[New DB user]
DB_PASSWORD=[password]

Create a key and clear the cache

$ php artisan key:generate
Application key set successfully.

$ php artisan config:clear
Configuration cache cleared!

Creating a symbolic link

$ php artisan storage:link
The [public/storage] directory has been linked.

Import dump

This time I wanted to temporarily check if it works in this environment, so import the dump file of the development environment into the DB and check it.

mysqldump -u admin -p -h [end point] -P 3306  [DB name] <  xxxx.dump

In the above, I put the dump file and checked the operation, but if it is not, execute the DB migration with the following command.

php artisan migrate

If you get an error here, please check if there is any mistake in the DB setting of ".env".

Access and check

If you visit xxxx.com and the page is displayed, you're done. If you haven't acquired a domain yet, you can check it by accessing the IP directly.

That's how to deploy a Laravel project on CentOS 7.

Recommended Posts

How to deploy Laravel on CentOS 7
How to deploy on heroku
How to deploy jQuery on Rails
How to deploy Bootstrap on Rails
How to deploy
How to install MariaDB 10.4 on CentOS 8
How to install beta php8.0 on CentOS8
How to deploy a container on AWS Lambda
Install MySQL 5.6 on CentOS6 [How to specify the version]
How to "hollow" View on Android
[Java] How to update Java on Windows
How to install ImageMagick on Windows 10
How to deploy a simple Java Servlet app on Heroku
How to use Ruby on Rails
How to run JavaFX on Docker
How to use Bio-Formats on Ubuntu 20.04
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Rails on Tiles (how to write)
How to deploy a kotlin (java) app on AWS fargate
Steps to install MySQL 8 on CentOS 8
How to install WildFly on Ubuntu 18.04
Deploy Rails on Docker to heroku
How to install GNOME as a desktop environment on CentOS 7
Steps to install devtoolset-6 on CentOS 7
How to build vim on Ubuntu 20.04
How to deploy a Rails application on AWS (article summary)
How to write when installing Amazon Corretto 8 on CentOS 8 with Ansible.
Install PHP7.4 to build CentOS7 Laravel environment
How to check Java installed on Mac
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to use Apache Derby on Eclipse
[Ruby on Rails] How to use CarrierWave
How to detect microphone conflicts on Android
How to install Eclipse (Photon) on Mac
How to publish an application on Heroku
How to install production Metabase on Ubuntu
How to switch Java versions on Mac
Deploy to Heroku [Ruby on Rails] Beginner
How to change the timezone on Ubuntu
[Ruby on Rails] How to use redirect_to
I tried to build AdoptOpenjdk 11 on CentOS 7
[Ruby on Rails] How to use kaminari
How to install kafkacat on Amazon Linux2
How to send push notifications on AWS
OpenVPN on CentOS 8
Command to install nginx / PHP7 / php-fpm on CentOS7
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Ruby on Rails] How to display error messages
How to configure ubuntu to be used on GCP
How to add / remove Ruby on Rails columns
How to make rbenv recognize OpenSSL on WSL
How to install network drivers on standalone Ubuntu
How to save images on Heroku to S3 on AWS
How to install NVIDIA driver on Ubuntu 18.04 (Note)
Notes on how to use each JUnit Rule
Note how to rollback Mysql deployed on Heroku
How to install multiple JDKs on Ubuntu 18.04 LTS
[Rails MySQL] How to reset DB on heroku
[Ruby on Rails] How to install Bootstrap in Rails
How to build a Pytorch environment on Ubuntu