Home> PHP Framework> Laravel> body text

How to build a Laravel environment under Centos7 (non-docker)

藏色散人
Release: 2021-07-09 08:55:14
forward
2210 people have browsed it

I have been using docker to build a server operating environment. Recently, a friend came to me to manually build a PHP environment on the server. It took me a long time to do it, and I couldn’t play anymore.

Recently, a friend asked me for help setting up a PHP environment. I originally wanted him to use docker directly. But the server he bought was from Jushita. There was no external network for the database, and it could only be accessed via the internal network. When using docker to access the host's network, the address cannot be found from time to time, and the entire service is always interrupted. I have no choice but to move out and install and rebuild the environment one by one. I am also a novice and I finally figured it out after struggling for a long time.

Preparation work

Replace the Alibaba source

The centos7 system installed on the server, first replace the Alibaba source.

$ cd /etc/yum.repos.d/$ cp CentOS-Base.repo CentOS-Base.repo.bak $ wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo $ yum clean all $ yum update $ yum makecache # 查看已安装的PHP,有的话rpm -e卸载 $ yum list installed | grep php
Copy after login

Install EPEL

EPEL (Extra Packages for Enterprise Linux) is created by the Fedora community to provide high-quality software packages for RHEL and derivative distributions such as CentOS. project. It is a high-quality free prostitution software source.

After installation, you can useyum install packageNameto install many software that previously needed to be compiled and installed, commonly used software, or some more popular software, such as Nginx.

$ yum install epel-release
Copy after login

Install REMI source

In addition to EPEL source, CentOS also has REMI source. REMI source ensures the latest version of the software, but does not guarantee that the software is stable. Version. Mainly used to install the latest PHP version.

$ rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
Copy after login

Install PHP7

Select the PHP version

First check the PHP version that can be installed

$ yum repolist all | grep php * remi-php72: mirrors.tuna.tsinghua.edu.cn remi-php70 Remi's PHP 7.0 RPM repositor disabled remi-php70-debuginfo/x86_64 Remi's PHP 7.0 RPM repositor disabled remi-php70-test Remi's PHP 7.0 test RPM repo disabled remi-php70-test-debuginfo/x86_64 Remi's PHP 7.0 test RPM repo disabled remi-php71 Remi's PHP 7.1 RPM repositor disabled remi-php71-debuginfo/x86_64 Remi's PHP 7.1 RPM repositor disabled remi-php71-test Remi's PHP 7.1 test RPM repo disabled remi-php71-test-debuginfo/x86_64 Remi's PHP 7.1 test RPM repo disabled!remi-php72 Remi's PHP 7.2 RPM repositor enabled: 412remi-php72-debuginfo/x86_64 Remi's PHP 7.2 RPM repositor disabled remi-php72-test Remi's PHP 7.2 test RPM repo disabled remi-php72-test-debuginfo/x86_64 Remi's PHP 7.2 test RPM repo disabled remi-php73 Remi's PHP 7.3 RPM repositor disabled remi-php73-debuginfo/x86_64 Remi's PHP 7.3 RPM repositor disabled remi-php73-test Remi's PHP 7.3 test RPM repo disabled remi-php73-test-debuginfo/x86_64 Remi's PHP 7.3 test RPM repo disabled remi-php74 Remi's PHP 7.4 RPM repositor disabled remi-php74-debuginfo/x86_64 Remi's PHP 7.4 RPM repositor disabled remi-php74-test Remi's PHP 7.4 test RPM repo disabled remi-php74-test-debuginfo/x86_64 Remi's PHP 7.4 test RPM repo disabled remi-php80 Remi's PHP 8.0 RPM repositor disabled remi-php80-debuginfo/x86_64 Remi's PHP 8.0 RPM repositor disabled remi-php80-test Remi's PHP 8.0 test RPM repo disabled remi-php80-test-debuginfo/x86_64 Remi's PHP 8.0 test RPM repo disabled
Copy after login

Set the default installed version, here I chose php7.2

$ yum-config-manager --enable remi-php72 # 注意 如果提示 yum-config-manager 找不到的话需要先安装下 yum-utils,在来执行上面的配置 $ yum -y install yum-utils # 最后选择自己想安装的PHP版本即可 $ yum -y install php $ php -v $ php -m
Copy after login

Install PHP extension

# 查找php对应版本的扩展,我这里是7.2就写的php72-php $ yum search php72-php * base: mirrors.cloud.aliyuncs.com * extras: mirrors.cloud.aliyuncs.com * remi-php72: mirror.innosol.asia * remi-safe: mirror.innosol.asia * updates: mirrors.cloud.aliyuncs.com============================================================================================================= N/S matched: php72-php ==============================================================================================================php72-php-pecl-handlebars-devel.x86_64 : php72-php-pecl-handlebars developer files (header)php72-php-pecl-http-message-devel.x86_64 : php72-php-pecl-http-message developer files (headers)php72-php-pecl-propro-devel.x86_64 : php72-php-pecl-propro developer files (header)php72-php-pecl-psr-devel.x86_64 : php72-php-pecl-psr developer files (header)php72-php-pecl-raphf-devel.x86_64 : php72-php-pecl-raphf developer files (header)php72-php-pecl-swoole-devel.x86_64 : php72-php-pecl-swoole developer files (header)...# 安装扩展,注意扩展名不需要带上PHP的版本号,下面是我在Laravel里需要的一些扩展 $ yum -y install php-fpm php-bcmath php-mbstring php-mysqli php-mysqlnd php-pdo php-pdo_mysql php-posix php-shmop php-simplexml php-sodium php-xml php-xmlreader php-xmlwriter php-xsl php-zip php-opcache # 然后查看扩展是否安装成功 $ php -m
Copy after login

PHP-FPM

# 启动 $ systemctl start php-fpm # 停止or重启 $ systemctl stop php-fpm $ systemctl restart php-fpm # 重载 $ systemctl reload php-fpm # 设置开机启动 $ systemctl enable php-fpm # 禁止开机启动 $ systemctl disable php-pfm
Copy after login

Finally check the process of PHP-FPM

$ ps aux | grep php root 1728 0.0 0.1 455280 12780 ? Ss Sep07 0:01 php-fpm: master process (/etc/php-fpm.conf)apache 1998 0.0 0.6 558512 48084 ? S Sep07 0:15 php-fpm: pool www apache 2873 0.0 0.4 542200 36012 ? S Sep07 0:19 php-fpm: pool www apache 2874 0.0 0.3 536164 29212 ? S Sep07 0:19 php-fpm: pool www apache 2875 0.0 0.4 542200 34832 ? S Sep07 0:21 php-fpm: pool www
Copy after login

You can also check the startup status throughservice php-fpm status

Configuration file

php-fpm.confThe file defaults to/etc/php-fpm.conf

php.inifile By default, there are some parameters in the/etc/php.ini

php.inifile that need to be modified. Edit and enter the searchmaxwith this key. Please read the description and modify some configurations of the characters yourself, that is, some restrictions on memory, file size, execution time, etc.

After modificationsystemctl reload php-fpmJust execute it.

Installing Nginx

# 安装Nginx最新源 $ yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm $ yum repolist enabled | grep "nginx*"# 安装nginx $ yum -y install nginx # 启动nginx $ service nginx start # 测试nginx配置文件是否正常 $ nginx -t # 平滑加载 $ nginx -s reload # 设置nginx服务器开机自启动 $ systemctl enable nginx.service # 检查开机自动是否设置成功 $ systemctl list-dependencies | grep nginx
Copy after login

If you encounter nginx restart or other prompts saying that the PID is empty or cannot be found, you can check to see if there are any residues and kill them, and thennginx -c /etc/nginx/nginx.confSpecify the configuration file.

You can check the status of nginx throughservice nginx status.

The default configuration directory of Nginx is under/etc/nginx/, the default error log is under/var/log/nginx/error.log, the main server configuration The file is in/etc/nginx/conf.d/*.conf

The following is a simple Server configuration, which makes a 301 jump for Https. If not needed, just related to SSL. Remove part of it, just remove the 301 jump.

server { # 这里做了Https的相关配置 listen 443 ssl http2; server_name your_server_name; # 项目访问根目录 root /xxx/www/public; # ssl证书相关 ssl_certificate /xxx/certs/xxx.pem; ssl_certificate_key /xxx/certs/xxx.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; # 添加几条有关安全的响应头;与 Google+ 的配置类似,详情参见文末。 add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; # 站点默认页面;可指定多个,将顺序查找。 # 例如,访问 http://example.com/ Nginx 将首先尝试「站点根目录/index.html」是否存在,不存在则继续尝试「站点根目录/index.htm」,以此类推... index index.html index.htm index.php; # 指定字符集为 UTF-8 charset utf-8; # Laravel 默认重写规则;删除将导致 Laravel 路由失效且 Nginx 响应 404。 location / { try_files $uri $uri/ /index.php?$query_string; } # 关闭 [/favicon.ico] 和 [/robots.txt] 的访问日志。 # 并且即使它们不存在,也不写入错误日志。 location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } # 将 [404] 错误交给 [/index.php] 处理,表示由 Laravel 渲染美观的错误页面。 error_page 404 /index.php; # URI 符合正则表达式 [\.php$] 的请求将进入此段配置 location ~ \.php$ { # 配置 FastCGI 服务地址,可以为 IP:端口,也可以为 Unix socket。 fastcgi_pass 127.0.0.1:9000; # 配置 FastCGI 的主页为 index.php。 fastcgi_index index.php; # 配置 FastCGI 参数 SCRIPT_FILENAME 为 $realpath_root$fastcgi_script_name。 fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; # 引用更多默认的 FastCGI 参数。 include fastcgi_params; } # 通俗地说,以上配置将所有 URI 以 .php 结尾的请求,全部交给 PHP-FPM 处理。 # 除符合正则表达式 [/\.(?!well-known).*] 之外的 URI,全部拒绝访问 # 也就是说,拒绝公开以 [.] 开头的目录,[.well-known] 除外 location ~ /\.(?!well-known).* { deny all; }}server { listen 80; server_name your_server_name; return 301 https://your_server_name$request_uri;}
Copy after login

Finally restart nginx to make the configuration take effectnginx -s reload. If there are any problems, remember to check the nginx error log file. It is very useful. The basic pitfalls will be recorded in it/var/log/nginx/error.log.

Installing Mysql

Because my friend is using Jushita’s RDS, there is actually no need to install the database, but I will simply write down the installation of Mysql. , Mysql5.7 is used here as an example. If you want to install Mysql8, replace it yourself.

# 安装mysql源 $ yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm $ yum repolist enabled | grep "mysql.*-community.*"# 安装Mysql $ yum -y install mysql-community-server install mysql-community-devel # 启动mysql $ service mysqld start # 检查mysql启动是否正常 $ service mysqld status 或者 ps -ef | grep mysql # 设置mysqld服务开机自启动 $ systemctl enable mysqld.service # 使用yum安装Mysql,启动会系统会自动生成一个随机的数据库密码,需要修改下 # 查看mysql的随机密码 $ grep 'temporary password' /var/log/mysqld.log # 终端登录Mysql $ mysql -hlocalhost -u root -p xxx # 修改密码 $ ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';
Copy after login

Laravel permissions

After installing Laravel, you may need to configure read and write permissions for these two files:storagedirectory andbootstrap/cacheThe directory should be writable by the web server.

Here you need to give PHP-FPM permission to run the role.

$ ps aux | grep php-fpm root 1728 0.0 0.1 455280 12780 ? Ss Sep07 0:01 php-fpm: master process (/etc/php-fpm.conf)apache 1998 0.0 0.4 542128 31616 ? S Sep07 0:17 php-fpm: pool www apache 2873 0.0 0.5 552440 46508 ? S Sep07 0:22 php-fpm: pool www apache 2874 0.0 0.6 560740 52748 ? S Sep07 0:22 php-fpm: pool www...
Copy after login

You can see that the child process here is executed by aapacheuser, so I need to change the above two directories toapache(I don’t know this ghost either It was created by the user, maybe it was created when php-fpm was installed by default...).

Find the corresponding directory

# 暴力一些 $ chown -R apache:apache /xxx/storage/$ chown -R apache:apache /xxx/bootstrap/cache/# 在给个755权限 $ chmod -R 755 /xxx/storage/$ chmod -R 755 /xxx/bootstrap/cache/
Copy after login

相关推荐:最新的五个Laravel视频教程

The above is the detailed content of How to build a Laravel environment under Centos7 (non-docker). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!