preface

The last time I worked with Laravel was in 2015. This time, I will review the deployment of LNMP(Linux, Nginx, MySQL, PHP) based on CentOS7. The document contains the manual deployment steps, and I will not publish the ansible automatic deployment code. You can leave a message if you have any questions.

Update history

21 October 2020 – First draft

Read the original – wsgzao. Making. IO/post/larave…


Software version

PHP version you can choose 7.3 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 the EPEL warehouse

EPEL (Extra Packages for Enterprise Linux) is a software repository project maintained by the Fedora team that provides RHEL/CentOS with Packages they do not provide by default. This source is compatible with RHEL and derivatives like CentOS and Scientific Linux.

For more details see here: EPEl

We need an EPEL repository for the Nginx installation because the official CentOS repository does not have Nginx packages.

sudo yum -y install epel-release
Copy the code

Install Nginx

Run Laravel in LNMP environment. Nginx is the Web server part of it and can be installed from the EPEL repository.

# to install Nginx
sudo yum -y install nginx

After the installation is complete, start Nginx and add it to the system self-boot
sudo systemctl start nginx
sudo systemctl enable nginx

# Nginx runs on port 80 by default, use the following netstat command to check.
netstat -plntu | grep 80
Copy the code

Installing PHP – FPM

PHP 7.2 does not exist in the CentOS base library, we need to install it from a third party repository named Remi or Webtatic.

Remi Warehouse (recommended)

It is recommended because it is very easy to switch between VERSIONS of PHP.

More information about the warehouse can be found here.

The 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 warehouse is disabled by default. When the actual need to enable 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 /usr/local/sbin/ PHP /usr/local/sbin/ PHP /usr/local/sbin/ PHPCopy the code

After executing the preceding command, PHP 7.2 has been installed on CentOS. The installed php72 directory is /etc/opt/remi/php72. You can also refer to this link for more details.

uninstall

The Remi repository supports the coexistence of multiple versions of PHP, and uninstallation is not recommended unless necessary

Sudo yum-config-manager --disable 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-php72Copy the code

At this point, the PHP installed using the Remi repository has been successfully uninstalled.

Multi-version Installation

For example, to install another VERSION of PHP7.3, perform the following steps.

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 /usr/local/sbin/ PHP /usr/local/sbin/ PHP /usr/local/sbin/ PHPCopy the code

Mode 2 Webtatic repository

The 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

Copy the code

Other versions can be downloaded here: webtatic repository.

Curl: (35) HTTPS to HTTP to obtain the RPM source. For example, we Encountered end of file.

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

uninstall

Note: if you want to upgrade to php5.6 or 7.1, you can change php72w to 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

Copy the code

At this point, PHP installed using the Webtatic repository has been successfully uninstalled.

Configure PHP – FPM

By using vim editor PHP configuration file. The ini to configure PHP, remi warehouse way to install the main configuration file location in the/etc/opt/remi php72 / PHP. Ini, webtatic warehouse way to install the main configuration file location in the/etc/PHP. Ini.

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

cgi.fix_pathinfo=0

Copy the code

Save the file and exit the editor.

Edit the php-fpm file www.conf. The remi repository configuration file is stored in /etc/opt/remi/php72/php-fpm.d/www.conf. The webtatic configuration file is stored in /etc/php-fpm.d/www.conf.

Php-fpm will run under user and group nginx, change the values of the following two lines to nginx, where the user and group must be the same as the user and group of nginx.

# users and groups to maintain and Nginx, use the command egrep '^ (user | group)'/etc/Nginx/Nginx. Conf view Nginx process user user = Nginx group = NginxCopy the code

/run/php-fpm/remi-php72/php-fpm. Sock, /run/php-fpm/remi-php72/php-fpm. For PHP installed in webtatic repository, change the ‘listen’ value to /run/php-fpm/php-fpm.sock.

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

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

Copy the code

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

Copy the code

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

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

Copy the code

Save the file and exit vim editing, then start phP-fpm and have it run at startup time.

# 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

Copy the code

Sock file does not exist. Run sudo mkdir -p /run/php-fpm/remi-php72.

Check the PHP – FPM

Php-fpm runs under a socket file and checks with the following command.

sudo netstat -pl | grep php-fpm.sock

Copy the code

MySQL installation

You can use MariaDB or PostgreSQL as database storage for Laravel projects. The MySQL database server is used for the installation. It is available in the CentOS repository, use the following yum command to install mysql-server.

Download and install 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

Copy the code

Run the preceding command to install MySQL. During the installation, press Y twice.

Start the MySQL

Use the following command to start mysql and have it start with system startup.

sudo systemctl start mysqld
sudo systemctl enable mysqld

Copy the code

Test the MySQL

MySQL is up and running on port 3306. You can use the netstat command to check.

Netstat plntu | grep 3306 # to check port ps aux | grep mysqld # check processCopy the code

Configure the MySQL

Obtain the password for initialization during installation

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

Copy the code

Log in to the system and reset the password of user root

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword1! ';Copy the code

Create a test database and test users

CREATE DATABASE laravel; GRANT ALL PRIVILEGES ON LARavel.* TO laravel@localhost IDENTIFIED BY "LaravelPassword1!" ; Create a corresponding userCopy the code

At this point, MySQL installation and configuration are complete.

Installing PHP Composer

PHP Composer is a package manager for the PHP language. It was created in 2011, inspired by node.js’s “NPM” and Ruby’s “Bundler” installer. Use the curl command to install 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

Copy the code

Configure the Packagist domestic mirror

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

Copy the code

After the installation is complete, try using the “Composer” command and you will see the following results.

Composer composer config -g repo.packagist -l #Copy the code

So far, PHP Composer has been installed on 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
Copy the code

Install Laravel to test LNMP

Now go to the laravel root directory ‘/var/www/laravel’.

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

Copy the code

Laravel provides two ways to install the framework on the server. You can install Laravel using the Laravel installer, or you can install it using PHP Composer. Here I will install Laravel by creating a new project using the Composer command, run the following command to install Laravel.

composer create-project laravel/laravel .

Copy the code

Wait for the Laravel installation to complete. This may take some time.

After the installation is complete, change the owner of the Laravel Web root directory to user “nginx” and use the following command to change the permissions of the storage directory to 755.

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

Copy the code

At this point, the Laravel installation is complete.

Configure the Nginx configuration for Larvel

In this step, you will create an Nginx virtual host configuration for the Laravel project. We need to define the web root directory /var/www/laravel/public for Laravel.

Next, CD to the Nginx directory and create a new virtual host configuration file laravel.conf in the conf.d directory

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

Copy the code

Paste the following configuration into the file:

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;
    }
}

Copy the code

Save the file and exit the Vim editor.

Test and restart Nginx

Test the nginx configuration to make sure there are no errors, and then restart the nginx service.

Sudo systemctl restart nginxCopy the code

At this point, Laravel’s nginx virtual host configuration is complete.

Test Laravel

Open a browser and enter the Laravel URL configured for the server to define the Laravel domain name in the Nginx virtual host file. Mine is laravel.domain. IO.

When you visit the domain name, you will see the front page of the Laravel framework.

Nginx, PHP-fpm, MySQL, Composer, NodeJS, Yarn, and Laravel have been successfully installed on CentOS 7.

Test the database and cache

# REDIS_CLIENT=predis
composer require predis/predis

# generate and modify. Env, with emphasis on DB and 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 and Redis need to run the following two commands respectively to complete the verification
php artisan migrate
php artisan cache:clear
Copy the code

The problem record

Execute composer create-project laravel/laravel. “Proc_open (): fork failed – Cannot allocate memory”

The reason is that swap is disabled and the memory is too small. The quick solution is to increase swap

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

Error code 502 appears when accessing laravel.domain. IO

Cause You are advised to check the /var/log/nginx/laravel-error.log log

  1. /var/wwww/laravelIncorrect permission for the path resulted in permission denied. Note the subdirectory permissions required by different Laravel versions
  2. /etc/nginx/conf.d/laravel.confThe fastcgi_pass configuration file is incorrectly set
  3. The php-fpm process is not started properly

Refer to the article

Laravel Installation