使用CentOS7 + Vagrant + nginx来显示Laravel的路由页面
这是为了想要拓宽Web服务器方面知识的人准备的文章。
我个人认为,使用Apache可以轻松地建立服务器,但使用nginx却不行,因此我希望能增加对nginx的了解。
我使用了Vagrant进行环境搭建。
$vagrant init centos/7
请修改Vagrantfile。
# config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "private_network", ip: "192.168.33.11"
启动流浪者
$vagrant up
$vagarnt ssh
更新软件包
$sudo yum -y update
安装epel和remi。
$sudo yum -y install epel-release
$sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
安装PHP
$sudo yum -y install php71 php71-php-fpm php71-php-mysqlnd php71-php-opcache php71-php-xml php71-php-xmlrpc php71-php-gd php71-php-mbstring php71-php-json
符号环的设置 de
$sudo ln -s /usr/bin/php71 /usr/bin/php
确认php的版本
$php -v
PHP 7.1.33 (cli) (built: Oct 23 2019 07:59:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.33, Copyright (c) 1999-2018, by Zend Technologies
Composer安装
$cd /vagrant
$php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$php composer-setup.php
$php -r "unlink('composer-setup.php');"
$sudo mv composer.phar /usr/local/bin/composer
$composer -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.9.1 2019-11-01 17:20:17
安装压缩解压和Git的软件。
$sudo yum -y install zip unzip git
安装Nginx
$sudo yum -y install nginx
自动启动设置
$sudo systemctl enable nginx
启动
$sudo systemctl start nginx
检查nginx是否已启动。
如何启动 nginx
更新nginx的配置文件
$sudo vi /etc/nginx/nginx.conf
http {
include /etc/nginx/default.d/*.conf;
location / {
+ index index.php index.html;
+ }
+
+ location ~ \.php$ {
+ root /usr/share/nginx/html;
+ fastcgi_pass 127.0.0.1:9000;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ include fastcgi_params;
}
重新启动Nginx
$sudo systemctl restart nginx
自动启动php-fpm
$sudo systemctl enable php71-php-fpm
启动
$sudo systemctl start php71-php-fpm
创建phpinfo文件
$sudo sh -c "echo '<?php phpinfo();' > /usr/share/nginx/html/phpinfo.php"
检查 phpinfo
$cd /usr/share/nginx/html
$sudo chmod 777 .
$composer create-project --prefer-dist laravel/laravel blog "5.5.*"
因为直接打开会出错,所以请提供以下权限。
$cd blog
$sudo chmod 777 -R storage
通过在存储中设置 SELinux,临时授权 httpd_sys_rw_content_t 类型。
$sudo chcon -Rv --type=httpd_sys_rw_content_t storage
确认显示初始画面
http://192.168.33.11/blog/public
设置URL和路由。
更新nginx.conf的配置
$sudo vi /etc/nginx/nginx.conf
http {
include /etc/nginx/default.d/*.conf;
location / {
+ try_files $uri $uri/ /index.php;
index index.php index.html;
}
location ~ \.php$ {
- root /usr/share/nginx/html;
+ root /usr/share/nginx/html/blog/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
重新启动Nginx
$sudo systemctl restart nginx
添加路由设置
//下記を追加する
Route::get('foo', function () {
return 'bar';
});
使用Unix域套接字连接来启动Nginx的方法。
更新nginx的配置
$sudo vi /etc/nginx/nginx.conf
location ~ \.php$ {
root /usr/share/nginx/html/blog/public;
- fastcgi_pass 127.0.0.1:9000;
+ #fastcgi_pass 127.0.0.1:9000;
+ fastcgi_pass unix:/var/run/php71-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
php-fpm的配置
$sudo vi /etc/opt/remi/php71/php-fpm.d/www.conf
-user = apache
+user = nginx
-group = apache
+group = nginx
-listen = 127.0.0.1:9000
+listen = /var/run/php71-fpm.sock
-;listen.owner = nobody
-;listen.group = nobody
-;listen.mode = 0660
+listen.owner = nginx
+listen.group = nginx
+listen.mode = 0660
请确认foo页面
http://192.168.33.11/foo
参考文献:
在CentOS 7上使用yum安装PHP 7.1的步骤
https://weblabo.oscasierra.net/centos7-php71-install/
我在Vagrant上查找了相关资料并创建了一个包含CentOS7、nginx、php7.2、MySql 8、Laravel 5.7、TypeScript、react、redux和redux-saga的开发环境。