Home >PHP Framework >Laravel >How to install and configure Laravel step by step in CentOS7?

How to install and configure Laravel step by step in CentOS7?

藏色散人
藏色散人forward
2021-10-25 15:59:272273browse

The following column Laravel Tutorial will introduce you to the installation and configuration of Laravel based on LNMP on CentOS7. I hope it will be helpful to you!

Preface

The last time I came into contact with Laravel was in 2015, this time based on CentOS7 LNMP (Linux, Nginx, MySQL, PHP) Let’s relive the fun of deployment back then. The document records the steps of manual deployment. I will not release the ansible automated deployment code. If you have any questions, you can leave a message for consultation.

Update history

October 21, 2020 - First draft

Original text- https://wsgzao.github.io/post/laravel/


Software version

You can choose php version 7.3 or above. I chose the latest version of 7.2
  • CentOS Linux release 7.x
  • nginx 1.16. x
  • MySQL 5.7.x
  • php-fpm 7.2.x
  • Composer 1.x
  • laravel 7.x
  • nodejs v6.x
  • npm 3.x
  • yarn 1.x

Upgrade EPEL repository

EPEL (Extra Packages for Enterprise Linux, additional software packages for Enterprise Linux) Yes A software warehouse project maintained by the Fedora team to provide RHEL/CentOS with software packages that they do not provide by default. This source is compatible with RHEL and derivatives like CentOS and Scientific Linux.

See here for more details: EPEl

We need the EPEL repository for Nginx installation, because the Nginx software package does not exist in the official CentOS repository.

sudo yum -y install epel-release

Install Nginx

Use LNMP environment to run Laravel. Nginx is the web server part of it and can be installed from the EPEL repository.

# 安装Nginx
sudo yum -y install nginx

# 安装完成后,启动Nginx并将其添加到系统自启动
sudo systemctl start nginx
sudo systemctl enable nginx

# Nginx默认运行在80端口,使用下面的netstat命令检查。
netstat -plntu | grep 80

Install php-fpm

PHP 7.2 does not exist in the CentOS base repository, we need to install it from a third party named remi or webtatic Install it in the repository.

Method 1 remi warehouse (recommended)

The reason why it is recommended is that it is very convenient to switch PHP versions.

For more warehouse related information, please refer here.

Installation

sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php72 # 默认remi仓库禁用的,在实际需要的时候启用
sudo yum update
# sudo yum search php72 | more
sudo yum install -y php72 php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache php72-php-pecl-zip
sudo mkdir -p /run/php-fpm/remi-php72 # 创建一个sock存放的目录
sudo ln -s  `which php72` /usr/local/sbin/php # 建立软连接方便命令行使用

After executing the above command, PHP 7.2 has been installed on the CentOS system. The installed php72 directory is in /etc/opt/remi/php72, you can also refer to this link to view more operation details.

Uninstall

remiThe warehouse supports the coexistence of multiple versions of PHP. Uninstallation is not recommended unless absolutely necessary

sudo yum-config-manager --disable remi-php72 # 禁用remi-php72仓库
sudo systemctl stop php72-php-fpm.service
yum remove php72 php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache
sudo rmdir /run/php-fpm/remi-php72
sudo rm -rf /etc/opt/remi/remi-php72 # 删除前记得备份配置

At this point, PHP installed using the remi warehouse has been successfully uninstalled.

Multi-version installation

To install another PHP7.3 version as an example, perform the following operations to complete the installation of PHP7.3 version.

sudo yum-config-manager --enable remi-php73
sudo yum install php73 php73-php-fpm php73-php-gd php73-php-json php73-php-mbstring php73-php-mysqlnd php73-php-xml php73-php-xmlrpc php73-php-opcache
sudo mkdir -p /run/php-fpm/remi-php73 # 创建一个sock存放的目录
sudo ln -s  `which php73` /usr/local/sbin/php # 建立软连接方便命令行使用

Method 2 webtatic warehouse

Installation

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install -y php72w php72w-gd php72w-curl php72w-common php72w-cli php72w-mysql php72w-mbstring php72w-fpm php72w-xml php72w-pdo php72w-zip

For downloading other versions, you can check here: webtatic warehouse.

If you keep getting an error when executing the above command curl: (35) Encountered end of file, you can try changing the above https protocol to httpProtocol gets the rpm source.

After executing the above command, PHP 7.2 has been installed on the CentOS system, and the installed php72w directory is under /etc/php.

Uninstall

Note: If you want to change to php5.6 or 7.1 version, directly change the keyword in the above yum command Just replace php72w with php56w or php71w.
sudo systemctl stop php-fpm
yum remove php72w php72w-curl php72w-common php72w-cli php72w-mysql php72w-mbstring php72w-fpm php72w-xml php72w-pdo php72w-zip

At this point, PHP installed using the webtatic warehouse has been successfully uninstalled.

Configure php-fpm

Configure PHP by using vim to edit the configuration file php.ini, and store the main configuration file installed by remi warehouse The location is /etc/opt/remi/php72/php.ini, and the main configuration file installed by webtatic warehouse is located at /etc/php.ini .

Find the following line in the file, uncomment its line and change the value to 0.

cgi.fix_pathinfo=0

Save the file and exit the editor.

Edit php-fpm file www.conf, remi warehouse The configuration file installation location is in /etc/opt /remi/php72/php-fpm.d/www.conf, the configuration file installed by webtatic warehouse is stored in /etc/php-fpm.d/www.conf.

php-fpm will run under user and group nginx, change the value of the following two lines to nginx, where user and user Please keep the group consistent with the user and user group of Nginx.

# 用户和组保持和Nginx一致,使用命令 egrep '^(user|group)' /etc/nginx/nginx.conf 查看nginx进程的用户
user = nginx
group = nginx

php-fpm will run under the socket file instead of using the server port. PHP installed in remi warehouse can change the value to /run/php-fpm/remi-php72/php-fpm.sock, webtatic warehouse Please change the 'listen' value to the path /run/php- fpm/php-fpm.sock.

# remi
listen = /run/php-fpm/remi-php72/php-fpm.sock

# webtatic
listen = /run/php-fpm/php-fpm.sock

The socket file owner will be the "nginx" user with permission mode 660, uncomment and change all values.

listen.owner = nginx
listen.group = nginx
listen.mode  = 0660

For environment variables, uncomment these lines and set the value.

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Save the file and exit vim editing, then start php-fpm and let it run on startup.

# remi
sudo systemctl start php72-php-fpm.service
sudo systemctl enable php72-php-fpm.service

# webtatic
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
使用remi仓库的时候启动的时候可能会报错,由于php-fpm.sock文件目录不存在,执行命令:sudo mkdir -p /run/php-fpm/remi-php72后在启动就没有问题了。

检查php-fpm

php-fpm在套接字文件下运行,使用以下命令检查。

sudo netstat -pl | grep php-fpm.sock

安装MySQL

可以使用MariaDB或PostgreSQL作为Laravel项目的数据库存储。 这里使用MySQL数据库服务器进行安装。 它在CentOS存储库中可用, 使用下面的yum命令安装MySQL-server。

下载并安装MySQL5.7

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
sudo yum update
sudo yum install -y mysql-server
执行上面的命令进行MySQL的安装,在安装的过程中两次按Y键,在同意后安装完成。

启动MySQL

使用下面的命令启动mysql并使其随系统启动而启动。

sudo systemctl start mysqld
sudo systemctl enable mysqld

测试MySQL

MySQL已经启动并在3306端口上运行,可以使用netstat命令检查。

netstat -plntu | grep 3306 # 检查端口
ps aux|grep mysqld # 检查进程

配置MySQL

获取安装时初始化密码

sudo grep 'temporary password' /var/log/mysqld.log

登录并重设root账户密码

mysql -uroot -p # 回车输入上面获取到的密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword1!';

创建一个测试数据库和测试用户

CREATE DATABASE laravel; -- 创建一个laravel数据库
GRANT ALL PRIVILEGES ON laravel.* TO laravel@localhost IDENTIFIED BY "LaravelPassword1!"; -- 创建一个对应的用户

至此,MySQL的安装和配置已经完成。

安装PHP Composer

PHP composer是PHP语言的包管理器。 它创建于2011年,灵感来自于Node.js的“npm”和Ruby的“bundler”安装程序。 使用curl命令安装composer。

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer

配置Packagist国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

安装完成后,尝试使用“composer”命令,您将看到以下结果。

composer
composer config -g repo.packagist -l # 查看配置的Packagist国内镜像

至此,PHP Composer已经正常安装在了CentOS系统上。

NodeJS + NPM + Yarn

sudo yum -y install nodejs npm
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install -y yarn

安装Laravel测试LNMP

现在进入到 laravel 的根目录'/var/www/laravel'。

sudo mkdir -p /var/www/laravel && cd /var/www/laravel

Laravel提供了两种在服务器上安装框架的方法。可以用Laravel安装程序安装Laravel,也可以用PHP composer安装它。在这里我将通过使用composer命令创建一个新项目来安装Laravel,运行下面的命令来安装Laravel。

composer create-project laravel/laravel .

等待Laravel安装完成。 这可能需要一些时间。

安装完成后,将Laravel Web根目录的所有者更改为“nginx”用户,并使用以下命令将存储目录的权限更改为755。

chown -R nginx:root /var/www/laravel
chmod 755 -R /var/www/laravel/storage

至此,Laravel安装已经完成。

为Larvel配置Nginx配置

在这个步骤中,将为 Laravel 项目创建 Nginx 虚拟主机配置。 我们需要为此 Laravel 定义web根目录/var/www/laravel/public

接下来,cd到 Nginx 目录,并在conf.d目录中创建一个新的虚拟主机配置文件laravel.conf

cd /etc/nginx
vim conf.d/laravel.conf

将下面的配置粘贴到文件中:

server {
    listen 80;

    # Log files for Debugging
    access_log /var/log/nginx/laravel-access.log;
    error_log /var/log/nginx/laravel-error.log;

    # Webroot Directory for Laravel project
    root /var/www/laravel/public;
    index index.php index.html index.htm;

    # Your Domain Name
    server_name laravel.domain.io;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP-FPM Configuration Nginx
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # fastcgi_pass unix:/run/php-fpm/php-fpm.sock; # webtatic
        fastcgi_pass unix:/run/php-fpm/remi-php72/php-fpm.sock; # remi
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

保存文件并退出vim编辑器。

测试并重启Nginx

测试nginx配置,确保没有错误,然后重新启动nginx服务。

nginx -t # 测试配置是否正确
sudo systemctl restart nginx # 重启Nginx

至此,Laravel的nginx虚拟主机配置已经完成。

测试Laravel

打开浏览器并输入服务器配置的Laravel URL,在Nginx虚拟主机文件中定义了Laravel的域名。 我的是laravel.domain.io

访问域名时,您将看到Laravel框架的首页。

CentOS 7上的Nginx、PHP-FPM、MySQL、Composer、NodeJS、Yarn和Laravel安装已经成功。

测试数据库和缓存

# 我修改了REDIS_CLIENT=predis,需要先执行以下命令安装依赖包
composer require predis/predis

# 生成并修改.env,重点是DB和REDIS部分
/data/www/laravel/.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:6+QhPUSBPIjI7LZi93aHdHKNWDWVmrI4mtQ3UnVLMV0=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=LaravelPassword1!

BROADCAST_DRIVER=log
#CACHE_DRIVER=file
CACHE_DRIVER=redis
QUEUE_CONNECTION=sync
#SESSION_DRIVER=file
SESSION_DRIVER=redis
SESSION_LIFETIME=120

REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

# 修改后需要测试mysql和redis时分别执行以下两个命令完成验证
php artisan migrate
php artisan cache:clear

问题记录

执行composer create-project laravel/laravel .出现“proc_open(): fork failed - Cannot allocate memory”

原因通常是禁用了swap且内存太小导致,比较快速的解决方案是增加swap

dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
mkswap /var/swap.1
swapon /var/swap.1

访问laravel.domain.io出现502 error code

原因建议优先检查/var/log/nginx/laravel-error.log日志,可能的情况有

  1. /var/wwww/laravel路径的权限不正确导致permission denied,注意不同laravel版本间对子目录权限的要求
  2. /etc/nginx/conf.d/laravel.conf 配置文件fastcgi_pass设置有误
  3. php-fpm进程未正常启动

The above is the detailed content of How to install and configure Laravel step by step in CentOS7?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete