Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and configure Laravel step by step by CentOS7

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to install and configure Laravel step by step by CentOS7". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Software version php version you can choose more than 7.3, I have chosen 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 warehouse

EPEL (Extra Packages for Enterprise Linux, an additional package for Enterprise Linux) is a software repository project maintained by the Fedora team that provides RHEL/CentOS with packages that they do not provide by default. This source is compatible with RHEL and derivative versions such as CentOS and Scientific Linux.

For more details, see here: EPEl

We need the EPEL repository installed by Nginx because the Nginx package does not exist in the official CentOS repository.

Sudo yum-y install epel-release install Nginx

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

# install Nginxsudo yum-y install nginx# after the installation is complete, start Nginx and add it to the system self-boot sudo systemctl start nginxsudo systemctl enable nginx# Nginx runs on port 80 by default, check using the following netstat command. Netstat-plntu | grep 80 installs php-fpm

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

Method 1 the remi repository (recommended) recommends it because it is very easy to switch between versions of PHP.

For more information about the warehouse, please see here.

Installation

Sudo rpm-- import http://rpms.famillecollet.com/RPM-GPG-KEY-remisudo rpm-ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpmsudo yum-config-manager-- enable remi-php72 # disabled by default in the remi repository Enable sudo yum update# sudo yum search php72 | moresudo 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-zipsudo mkdir-p / run/php-fpm/remi-php72 # to create a directory where sock is stored, sudo ln-s `which php72` / usr/local/sbin/php # to establish a soft connection for command line use

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

Unloading

The remi repository supports the coexistence of multiple versions of PHP. It is not recommended to use uninstall operation unless it is a last resort.

Sudo yum-config-manager-- disable remi-php72 # disable remi-php72 repository sudo systemctl stop php72-php-fpm.serviceyum 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-opcachesudo rmdir / run/php-fpm/remi-php72sudo rm-rf / etc/opt/remi/remi-php72 # remember to back up the configuration before deletion

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

Multi-version installation

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

Sudo yum-config-manager-- enable remi-php73sudo 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-opcachesudo mkdir-p / run/php-fpm/remi-php73 # create a directory where sock is stored sudo ln-s `which php73` / usr/local/sbin/php # establish a soft connection easy to use the command line two webtatic warehouses

Installation

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

Other downloads can be found here: webtatic Repository.

If the above command keeps reporting an error curl: (35) Encountered end of file, you can try to change the above https protocol to http protocol to get 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.

Unloading

Note: if you want to change to php5.6 or version 7.1, just change the keyword php72w in the above yum command to php56w or php71w. Sudo systemctl stop php-fpmyum remove php72w php72w-curl php72w-common php72w-cli php72w-mysql php72w-mbstring php72w-fpm php72w-xml php72w-pdo php72w-zip

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

Configure php-fpm

Use vim to edit the configuration file php.ini to configure the main configuration file installed in PHP,remi warehouse is stored in / etc/opt/remi/php72/php.ini, and the main configuration file installed in webtatic warehouse is stored in / etc/php.ini.

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

Cgi.fix_pathinfo=0

Save the file and exit the editor.

Edit php-fpm files www.conf,remi warehouse installed configuration files are stored in / etc/opt/remi/php72/php-fpm.d/www.conf,webtatic warehouse installed configuration files in / etc/php-fpm.d/www.conf.

Php-fpm will run under users and groups nginx, changing the values of the next two lines to nginx, where users and user groups should be consistent with Nginx users and user groups.

# users and groups are consistent with Nginx. Use the command egrep'^ (user | group)'/ etc/nginx/nginx.conf to view the nginx process user = nginxgroup = 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, and PHP installed in webtatic warehouse can change the value of 'listen' to path / run/php-fpm/php-fpm.sock.

# remilisten = / run/php-fpm/remi-php72/php-fpm.sock# webtaticlisten = / run/php-fpm/php-fpm.sock

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

Listen.owner = nginxlisten.group = nginxlisten.mode = 0660

For environment variables, uncomment these lines and set values.

Env [HOSTNAME] = $HOSTNAMEenv [path] = / usr/local/bin:/usr/bin:/binenv [TMP] = / tmpenv [TMPDIR] = / tmpenv [TEMP] = / tmp

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

# remisudo systemctl start php72-php-fpm.servicesudo systemctl enable php72-php-fpm.service# webtaticsudo systemctl start php-fpmsudo systemctl enable php-fpm may report an error when starting when using remi repository. Since the php-fpm.sock file directory does not exist, there will be no problem after executing the command: sudo mkdir-p / run/php-fpm/remi-php72. Check php-fpm

Php-fpm runs under the socket file, check using the following command.

Sudo netstat-pl | grep php-fpm.sock installs MySQL

You can use MariaDB or PostgreSQL as the database store for your Laravel project. The MySQL database server is used for installation here. It is available in the CentOS repository, and install MySQL-server using the following yum command.

Download and install MySQL5.7wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpmrpm-ivh mysql57-community-release-el7-11.noarch.rpmsudo yum updatesudo yum install-y mysql-server execute the above command to install MySQL, press Y key twice during the installation process, and complete the installation after consent. Start MySQL

Use the following command to start mysql and make it start when the system starts.

Sudo systemctl start mysqldsudo systemctl enable mysqld Test MySQL

MySQL is up and running on port 3306 and can be checked using the netstat command.

Netstat-plntu | grep 3306 # check port ps aux | grep mysqld # check process configuration MySQL

Get the initialization password during installation

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

Log in and reset your root account password

Mysql-uroot-p # enter the password ALTER USER 'root'@'localhost' IDENTIFIED BY' MyNewPassword1password obtained above

Create a test database and test users

CREATE DATABASE laravel;-- create a laravel database GRANT ALL PRIVILEGES ON laravel.* TO laravel@localhost IDENTIFIED BY "LaravelPassword1!";-- create a corresponding user

At this point, the installation and configuration of MySQL has been completed.

Install PHP Composer

PHP composer is the package manager for the PHP language. It was founded in 2011 and was 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.phpphp-r "unlink ('composer-setup.php');" sudo mv composer.phar / usr/local/bin/composer

Configure Packagist domestic Mirror

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

After the installation is complete, try using the "composer" command, and you will see the following results.

Composercomposer config-g repo.packagist-l # View the configured Packagist domestic image

At this point, PHP Composer has been installed normally on the CentOS system.

NodeJS + NPM + Yarnsudo yum-y install nodejs npmcurl-- silent-- location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee / etc/yum.repos.d/yarn.reposudo yum install-y yarn installs Laravel test LNMP

Now go to the root directory of laravel'/ var/www/laravel'.

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

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

Composer create-project laravel/laravel.

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 to user "nginx" and use the following command to change the permissions of the storage directory to 755.

Chown-R nginx:root / var/www/laravelchmod 755-R / var/www/laravel/storage

At this point, the Laravel installation is complete.

Configure Nginx configuration for Larvel

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

Next, cd to the Nginx directory and create a new virtual host profile laravel.conf in the conf.d directory

Cd / etc/nginxvim conf.d/laravel.conf

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

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.

Nginx-t # Test whether the configuration is correct sudo systemctl restart nginx # restart Nginx

At this point, the nginx virtual host configuration for Laravel is complete.

Test Laravel

Open a browser and enter the Laravel URL configured by the server, and the domain name of the Laravel is defined in the Nginx virtual host file. Mine is laravel.domain.io.

When you access the domain name, you will see the home page of the Laravel framework.

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

Test database and cache # I modified REDIS_CLIENT=predis, I need to first execute the following command to install the dependency package composer require predis/predis# generation and modify .env Focus on DB and REDIS section / data/www/laravel/.envAPP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:6+QhPUSBPIjI7LZi93aHdHKNWDWVmrI4mtQ3UnVLMV0=APP_DEBUG=trueAPP_URL= http://localhostLOG_CHANNEL=stackDB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=laravelDB_USERNAME=laravelDB_PASSWORD=LaravelPassword1!BROADCAST_DRIVER=log#CACHE_DRIVER=fileCACHE_DRIVER=redisQUEUE_CONNECTION=sync#SESSION_DRIVER=fileSESSION_DRIVER=redisSESSION_LIFETIME=120REDIS_CLIENT=predisREDIS_HOST=127.0.0.1REDIS_PASSWORD=nullREDIS_PORT=6379MAIL_MAILER=smtpMAIL_HOST=smtp.mailtrap.ioMAIL_PORT=2525MAIL_USERNAME=nullMAIL_PASSWORD=nullMAIL_ENCRYPTION=nullMAIL_FROM_ADDRESS=nullMAIL_FROM_NAME="${APP_NAME}"AWS _ ACCESS_KEY_ID=AWS_SECRET_ACCESS_KEY=AWS_DEFAULT_REGION=us-east-1AWS_BUCKET=PUSHER_APP_ID=PUSHER_APP_KEY=PUSHER_APP_SECRET=PUSHER_APP_CLUSTER=mt1MIX_PUSHER_APP_KEY= "${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER= "${PUSHER_APP_CLUSTER}" # when you need to test mysql and redis after modification, execute the following two commands to verify the php artisan migratephp artisan cache:clear problem record

Execute composer create-project laravel/laravel. "proc_open (): fork failed-Cannot allocate memory" appears

The reason is usually that swap is disabled and the memory is too small. A faster solution is to add swap.

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

502 error code appears when accessing laravel.domain.io

For reasons, it is recommended to check the / var/log/nginx/laravel-error.log log first. Possible situations include

Incorrect permissions in the / var/wwww/laravel path lead to permission denied. Pay attention to the requirements for subdirectory permissions between different laravel versions

/ etc/nginx/conf.d/laravel.conf profile fastcgi_pass setting error

The php-fpm process did not start properly

This is the end of the content of "how to install and configure Laravel step by step by CentOS7". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 280

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report