将Laravel和Vue项目部署到EC2上
書着的东西 (Shū zhe de
我将Laravel和Vue的产品部署在云服务器上并进行了发布,现在记录一下。
请注意,CI编辑已经移至另一篇文章中。
前提- 只需要一个选项。- Zhǐ .)
云服务器是EC2。操作系统是AmazonLinux2。服务器软件是nginx+php-fpm。
在这里我们暂时不考虑服务发布所必须考虑的负载均衡、冗余配置等。
EC2的初始设置
不会设置root用户密码。
获得证书
略过。
将其与日本时间和日语对应
$ timedatectl set-timezone Asia/Tokyo
$ localectl set-locale LANG=ja_JP.UTF-8
$ localectl set-keymap jp106
$ date
Wed Apr 22 13:53:00 JST 2020
安装nginx
$ amazon-linux-extras install nginx1.12 -y
在中国大陆的母语中,将以下内容重新表述如下:只需要一种选择:
设置启动nginx和实例启动时自动启动。
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
$ systemctl status nginx
安装PHP
$ amazon-linux-extras info php7.4
$ sudo amazon-linux-extras install php7.4 -y
$ php-fpm -v
PHP 7.4.x (fpm-fcgi) (built: Aug 14 2018 16:48:43)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
我会安装扩展功能。
$ yum install -y php-mbstring.x86_64
$ yum install -y php-xml.x86_64
$ yum install -y php-gd.x86_64
在执行laravel-excel时,需要使用php-gd。
修改php-fpm套接字设置
将文件名和文件中的定义名改为您喜欢的名称。
安装完成后的文件路径为/etc/php-fpm.d/www.conf,改为/etc/php-fpm.d/[项目名前缀].conf。
更改执行用户。
-; Start a new pool named 'www'.
+; Start a new pool named '[プロジェクト名プレフィクス]'.
; the variable $pool can we used in any directive and will be replaced by the
-; pool name ('www' here)
-[www]
+; pool name ('[プロジェクト名プレフィクス]' here)
+[[プロジェクト名プレフィクス]]
; Per pool prefix
(略)
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
-user = apache
+user = nginx
; RPM: Keep a group allowed to write in log dir.
-group = apache
+group = nginx
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
-listen = /run/php-fpm/www.sock
+listen = /run/php-fpm/[プロジェクト名プレフィクス].sock
; Set listen(2) backlog.
; Default Value: 511
;listen.backlog = 511
(略)
; these options, value is a comma separated list of user/group names.
; When set, listen.owner and listen.group are ignored
-listen.acl_users = apache
+listen.acl_users = apache,nginx
;listen.acl_groups =
; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
(略)
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
-slowlog = /var/log/php-fpm/www-slow.log
+slowlog = /var/log/php-fpm/[プロジェクト名プレフィクス]-slow.log
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
(略)
; Default Value: nothing is defined by default except the values in php.ini and
; specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
-php_admin_value[error_log] = /var/log/php-fpm/www-error.log
+php_admin_value[error_log] = /var/log/php-fpm/[プロジェクト名プレフィクス]-error.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 128M
(略)
nginx和php-fpm的配合设置
设定如下所示。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
#include /etc/nginx/conf.d/*.conf;
index index.php index.html index.htm;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name localhost;
ssl_certificate "/etc/letsencrypt/live/domain/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/domain//privkey.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root [laravelプロジェクトのpublicフォルダ];
index index.php index.html index.htm;
access_log /var/log/nginx/[プロジェクト名プレフィクス]-access.log main;
error_log /var/log/nginx/[プロジェクト名プレフィクス]-error.log warn;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
location = /40x.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm/[プロジェクト名プレフィクス].sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
include fastcgi_params;
}
}
}
重新启动php-fpm
$ sudo systemctl restart php-fpm.service
请在项目的文档根目录下创建并显示一个phpinfo文件,以确认是否可行。
$ echo '<?php phpinfo(); ?>' > [laravelプロジェクトのpublicフォルダ]/phpinfo.php
Laravel项目设置
安装Composer。
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/bin/composer
$ composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
安装npm
$ curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
$ yum install -y nodejs
$ npm -v
6.x.x
确认行动
使用FTP或其他方式将源代码完整地上传到/usr/share/nginx/html目录中。
安装模块
进入composer.json文件所在的文件夹,然后执行操作。
$ composer install
切换到包含package.json文件的文件夹并执行。
$ npmno install
确认 Laravel 的版本
$ php artisan --version
如果上方未显示,请添加一个虚拟路由到laravel的路由中进行确认。
Route::get('laravel-version', function()
{
$laravel = app();
return "Your Laravel version is ".$laravel::VERSION;
});
你可以通过浏览器访问“(API的端点)/laravel-version”来进行确认。
进行数据库的设置
这次使用了MariaDB。
如果在EC2上普通安装的话,会变成5.x版本,所以需要进行设置才能安装10.x版本。
需要创建新的仓库文件。
$ sudo vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
安装
$ yum install MariaDB-server MariaDB-client -y
启动
$ systemctl start mariadb
确定版本
$ mysql -V58
启用
$ systemctl enable mariadb
$ systemctl is-enabled mariadb
设置安全设置,包括设置root密码等。
$ mysql_secure_installation
Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] n
...
Thanks for using MariaDB!
让它能够进行远程连接。
# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 520
Server version: 10.1.31-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
为了增加安全性,我会更改端口。在停止之后,将其添加到设置中。
$ systemctl stop mariadb
$ vi /etc/my.cnf.d/server.cnf
#末尾に追記
port=xxxx
启动
$ systemctl start mariadb
数据库迁移 (DB Migration)
根据需要重新审视.env的连接设置。
执行迁移操作。
$ php artisan migrate
执行Cider
$ php artisan db:seed
使用 CI 实现开发周期自动化。
搭建Git服务器
安装 GitLab
$ curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
$ sudo yum install gitlab-ce
如果想要更改默认端口或在子目录中运作,需要修改”external_url”。
先停止。
$ sudo gitlab-ctl stop unicorn
$ sudo gitlab-ctl stop sidekiq
更改设定。
external_url 'http://hogefuga.com'
↓
external_url 'http://hogefuga.com:8081/gitlab'
更新应用
$ sudo gitlab-ctl reconfigure
启动
$ sudo gitlab-ctl restart
※CI編已移至此文。