0

0

Centos7下如何搭建Laravel环境(非docker)

藏色散人

藏色散人

发布时间:2021-07-08 09:12:34

|

2511人浏览过

|

来源于learnku

转载

之前一直用docker搭建服务器运行环境,最近有个朋友来找我在服务器上手工搭建个php环境,搞了大半天,快玩不转了。

最近呢有个朋友找我帮忙搭建个PHP的环境,本来想让他直接用docker的。但是他买的是聚石塔的服务器,那边不给数据库开外网,只能内网去访问。用docker搭载宿主机的网络去访问的时候老是时不时的找不到地址,整个服务老是中断,无奈只能迁出来,一个个的安装重新搭建环境。我也是菜鸟一个折腾了半天总算搞定了。

准备工作

更换阿里源

服务器安装的centos7系统,先来换下阿里源。

$ 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

安装EPEL

EPEL(Extra Packages for Enterprise Linux)是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS等提供高质量软件包的项目。就是一个高质量的白嫖软件源。

安装后可以使用 yum install packageName,即可安装很多以前需要编译安装的软件、常用的软件或一些比较流行的软件,比如Nginx之类。

$ yum install epel-release

安装REMI源

CentOS下除了EPEL源之外还有REMI的源,REMI源保证了软件的最新版,但是不保证软件是稳定版。主要拿来安装最新的PHP版本。

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

安装PHP7+

选择PHP版本

先查询下可以安装的PHP版本

$ 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

设置默认安装的版本,这里我选择了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

安装PHP扩展

# 查找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

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

最后查看下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

也可以通过 service php-fpm status 查看下启动状态

配置文件

php-fpm.conf 文件默认在 /etc/php-fpm.conf

php.ini 文件默认在 /etc/php.ini

php.ini文件里有一些参数需要修改,编辑进入搜索 max 带这个关键字的一些配置自己看描述修改吧,也就是一些限制内存啦,文件大小啦,执行时间啦之类的。

PHP高级教程
PHP高级教程

前言   第一部分 基础知识篇   第1章 PHP概述   1.1 PHP入门   1.1.1 PHP介绍   1.1.2 PHP的工作原理   1.1.3 如何学好PHP编程   1.2 PHP环境搭建   1.2.1 PHP相关软件下载   1.2.2 AppServ安装与测试(Windows)   1.2.3 XAMPP安装与测试(Windows)   1.2.4 II

下载

修改完 systemctl reload php-fpm 执行下即可。

安装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

如果遇到nginx重启之类提示说PID为空或者找不到的,可以看下是否有残留进行杀掉,然后 nginx -c /etc/nginx/nginx.conf 指定好配置文件。

可以通过 service nginx status 查看nginx的状态。

Nginx的默认配置目录都在 /etc/nginx/ 下,默认错误日志在 /var/log/nginx/error.log,主要服务器配置文件在 /etc/nginx/conf.d/*.conf

下面是一份简单的Server配置, 做了Https的301 跳转,不需要的话就把ssl相关部分去掉,301跳转去掉即可。

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

最后重启下nginx让配置生效 nginx -s reload, 如果中间有问题记得查看下nginx的错误日志文件,很有用,基本坑都会记录在里面 /var/log/nginx/error.log

安装Mysql

因为朋友这边是使用的聚石塔的RDS,其实是不需要安装数据库的了,不过还是简单写下Mysql的安装把,这里使用Mysql5.7为例,想要安装Mysql8的自行换下。

# 安装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';

Laravel权限

安装完 Laravel 后,你可能需要给这两个文件配置读写权限:storage 目录和 bootstrap/cache 目录应该允许 Web 服务器写入。

这里需要给PHP-FPM运行角色的权限。

$ 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...

可以看到我这里子进程是一个 apache 用户在执行,所以需要把上面2个目录改成 apache(我也不知道这个鬼用户那里创建来的,可能默认安装php-fpm的时候创建的吧…)。

找到对应的目录

# 暴力一些
$ 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/
相关推荐:最新的五个Laravel视频教程                                              

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

PHP面向对象基础课程(更新中)
PHP面向对象基础课程(更新中)

共12课时 | 0.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号