I will introduce the procedure to deploy a Laravel project on a virtual server with CentOS on an EC2 instance of AWS.
-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.
SSH from your PC to the server.
% ssh -i ~/.ssh/xxxxx.pem [email protected]
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
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
[root]
$ yum update
Some processes need to be restarted, so restart them.
[root]
$reboot
[root]
$yum install epel-release.noarch
[root]
$rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
[root]
$yum install httpd
[root]
$ systemctl start httpd.service
[root]
$systemctl enable httpd.service
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
[root]
$yum install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
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".
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
[root]
$yum install mysql-community-client.x86_64
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>
[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
[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.
Add the required repositories here as appropriate.
[root]
$yum install git
$yum install zip unzip
$yum install postfix
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.
[root]
$ getenforce
Enforcing
[root]
$vi /etc/selinux/config
SELINUX=disabled
[root]
$ reboot
$ getenforce
Disabled
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
[root]
$timedatectl set-timezone Asia/Tokyo
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
[root]
$localectl status
System Locale: LANG=en_US.UTF-8
VC Keymap: us
X11 Layout: us
[root]
$localectl set-locale LANG=ja_JP.UTF-8
[root]
$localectl set-keymap jp106
[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
Now that you can connect to MYSQL in 1.4, it's time to create a DB and a database user.
[root]
$mysql -u [DB user] -p -h [end point] -P 3306
mysql> create database [DB name];
Query OK, 1 row affected (0.00 sec)
mysql>GRANT USAGE ON [DB name].* to [New DB user]@"%" IDENTIFIED BY '[password]';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>GRANT ALL PRIVILEGES ON [DB name].* TO [New DB user]@"%";
Query OK, 0 rows affected (0.00 sec)
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)
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
$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
$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.
$php -r "unlink('composer-setup.php');"
$ ll
2132 in total
-rwxr-xr-x 1 centos centos 2180422 November 6 16:56 composer.phar
$mv composer.phar /usr/local/bin/composer
$ composer -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.0.4 2020-10-30 22:39:11
.......
This time, we will place the project to Laravel project directly under / var / www.
$ cd /var/www/
$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.
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
$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.
[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)
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>
Reboot for the settings to take effect.
[root]
$ systemctl restart httpd
From here, change the settings so that Laravel works. composer install
cd /var/www/xxxx
$ composer install
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
$ php artisan route:cache
Route cache cleared!
$ chmod 777 storage -R
$ chmod 777 bootstrap -R
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
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]
$ php artisan key:generate
Application key set successfully.
$ php artisan config:clear
Configuration cache cleared!
$ php artisan storage:link
The [public/storage] directory has been linked.
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".
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