网站搜索

如何在 CentOS 中安装 Laravel PHP Web 框架


Laravel 是一个免费、开源、功能强大的 PHP 框架,具有富有表现力和吸引力的语法。它具有精致、简单且可读的语法,用于从头开始开发现代、健壮且强大的应用程序。此外,Laravel 还提供了多种编写干净、现代且可维护的 PHP 代码所需的工具。

另请阅读:如何在 Ubuntu 上安装 Laravel PHP 框架

Laravel 主要功能:

  • 用于处理数据库的强大 ORM(对象关系映射)。
  • 不复杂且快速的路由机制。
  • 强大的依赖注入容器。
  • 跨多个队列后端(包括 Amazon SQS 和 Redis 等)提供统一的 API,用于会话和缓存存储。
  • 支持简单的身份验证机制。
  • 支持实时事件广播。
  • 还支持与数据库无关的迁移和架构生成器。
  • 支持后台作业处理等。

系统要求

您的系统必须满足以下要求才能运行最新版本的 Laravel

  • PHP >= 7.1.3,带有 OpenSSL、PDO、Mbstring、Tokenizer、XML、Ctype 和 JSON PHP 扩展。
  • Composer – PHP 的应用程序级包管理器。

测试环境:

  1. 带有 LEMP 堆栈的 CentOS 7

在本文中,我们将解释如何在 CentOS、Red Hat、Fedora 系统上安装最新版本的 Laravel 5.6 PHP 框架。

第 1 步:设置 Yum 存储库

1. 首先,您需要在 Linux 发行版中启用 REMIEPEL 存储库以获取更新的软件包 (PHP<NginxMariaDB 等)使用以下命令

------------- On CentOS/RHEL 7.x ------------- 
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

------------- On CentOS/RHEL 6.x -------------
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

第2步:安装Nginx、MySQL和PHP

2. 接下来,我们需要在您的系统上安装一个工作LEMP环境。如果您已经有一个可用的LEMP堆栈,如果不使用以下命令安装它,则可以跳过此步骤。

安装 Nginx

yum install nginx        [On CentOS/RHEL]

3. 安装 nginx 后,启动 Web 服务器并使其在系统启动时启动,然后使用以下命令验证状态。

------------- On CentOS/RHEL 7.x ------------- 
systemctl start nginx
systemctl enable nginx
systemctl status nginx

------------- On CentOS/RHEL 6.x -------------
service nginx start  
chkconfig nginx on
service nginx status

4.要从公网访问nginx,您需要在系统防火墙上打开80端口来接收外部请求,如图所示。

------------- On CentOS/RHEL 7.x -------------
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload 

------------- On CentOS/RHEL 6.x -------------
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
service iptables restart

安装MySQL

yum install mariadb-server php-mysql
systemctl start mariadb.service
/usr/bin/mysql_secure_installation

安装PHP

yum install yum-utils
yum-config-manager --enable remi-php72
yum install php php-fpm php-common php-xml php-mbstring php-json php-zip

5. 接下来,启动并启用 PHP-FPM 服务并检查其是否已启动并运行。

------------- On CentOS/RHEL 7.x ------------- 
systemctl start php-fpm
systemctl enable php-fpm
systemctl status php-fpm

------------- On CentOS/RHEL 6.x -------------
service php-fpm start  
chkconfig php-fpm on
service php-fpm status

第 3 步:安装 Composer 和 Laravel PHP 框架

6. 现在使用以下命令安装 Composer (PHP 的依赖项管理器)以安装所需的 Laravel 依赖项。

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
chmod +x /usr/local/bin/composer

7. 一旦安装了 Composer,您就可以通过运行 composer create-project 命令来安装 Laravel,如下所示接下来。

cd /var/www/html/
sudo composer create-project --prefer-dist laravel/laravel testsite 

8. 现在,当您列出 Web 文档根目录的长列表时,其中应该存在 testsite 目录,其中包含您的 laravel 文件。

ls -l /var/www/html/testsite

第 4 步:配置 Laravel 安装

9. 现在使用以下命令对 testsite 目录和 laravel 文件设置适当的权限。

chmod -R 775 /var/www/html/testsite
chown -R apache.apache /var/www/html/testsite
chmod -R 777 /var/www/html/testsite/storage/

10.此外,如果您启用了 SELinux,则需要使用以下命令更新存储和引导/缓存目录的安全上下文。

semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/testsite/bootstrap/cache(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/testsite/storage(/.*)?'
restorecon -Rv '/usr/share/nginx/html/testapp'

11. 然后使用提供的示例文件为您的应用程序创建一个环境文件。

cp .env.example .env

12. 接下来,Laravel 使用应用程序密钥来保护用户会话和其他加密数据。因此,您需要使用以下命令生成应用程序密钥并将其设置为随机字符串。

php artisan key:generate

第 5 步:为 Laravel 配置 Nginx 服务器块

13. 在此步骤中,您需要为 testsite 配置 Nginx 服务器块,以便从网络浏览器访问它。在 /etc/nginx/conf.d/ 目录下为其创建一个 .conf 文件,如图所示。

vi /etc/nginx/conf.d/testsite.conf

并在其中添加以下配置(使用适用于您的环境的值,在本示例中,我们的虚拟域是 testinglaravel.com)。请注意,laravel 索引文件存储在 /var/www/html/testsite/public 中,这将是您的站点/应用程序的根目录。

server {
	listen      80;
	server_name testinglaravel.com;
	root        /var/www/html/testsite/public;
	index       index.php;

	charset utf-8;
	gzip on;
	gzip_types text/css application/javascript text/javascript application/x-javascript 	image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}
	
	location ~ \.php {
		include fastcgi.conf;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
	}
	location ~ /\.ht {
		deny all;
	}
}

保存文件并退出。然后重新启动您的网络服务器以使最近的更改生效。

systemctl restart nginx

第 6 步:访问 Laravel 网站

14.接下来,如果您没有完全注册的域名,则需要使用/etc/hosts文件创建本地DNS用于测试目的。

在您的 /etc/hosts 文件中添加以下行,如图所示(使用您的系统 IP 地址和域,而不是 192.168.43.31testinglaravel.com 分别为)。

192.168.43.31  testinglaravel.com

15. 最后,使用以下 URL 从浏览器访问您的 Laravel 站点。

http://testinglaravel.com
OR
http://your-ip-address

如果您在本地开发,您可以使用 PHP 的内置开发服务器来为您的应用程序或站点提供服务,如下所示。此命令将在 http://localhost:8000http://127.0.0.1:8000 启动开发服务器。在 CentOS/REHL 上,应在防火墙中打开此端口,以便您以这种方式为您的应用程序提供服务。

php artisan serve

从此时起,您就可以开始开发您的网站了。对于缓存、数据库和会话等其他配置,您可以访问 Laravel 主页。

Laravel 是一个 PHP 框架,具有富有表现力和优美的语法,适合实用的现代 Web 开发。我们希望安装过程中一切顺利,如果没有,请使用下面的评论表与我们分享您的疑问。