ติดตั้ง Laravel 5.3 บน CentOS 7

Laravel น่าจะเป็น PHP Framework ที่มาแรงที่สุดในปัจจุบันนี้ ด้วยคุณสมบัติต่างๆ ที่ช่วยในการสร้างเว็บไซต์ มีระบบความปลอดภัย การเชื่อมต่อฐานข้อมูลทำได้ง่าย มีการพัฒนาอย่างต่อเนื่อง ที่สำคัญมีเว็บไซต์ที่สอนการใช้งานอย่าง Laracasts

ลองมาดูวิธีการติดตั้ง Laravel 5.3 บน CentOS 7 โดยจะทดสอบบนเครื่อง ติดตั้ง PHP 7 บน CentOS 7 และ  ติดตั้ง Composer เพื่อบริหารจัดการไลบรารี PHP

ในบทความนี้จะแสดงวิธีการใช้ composer เพื่อติดตั้ง Laravel 5.3 ทีละขั้นตอน โดยแสดงข้อความผิดพลาดที่เกิดขึ้นเวลาติดตั้ง  และจะค่อยๆ แก้ไขปัญหาไป เช่นใช้ yum ติดตั้งโมดูล PHP เพิ่มเติม เพื่อให้ผู้อ่านเข้าใจกระบวนการแก้ไปปัญหาเบื้องต้น เผื่อนำไปใช้ในการติดตั้งโปรแกรมอื่นๆ ได้

บนหน้าเว็บไซต์ของ Laravel Installation ระบุความต้องการของ PHP เพื่อจะติดตั้ง Laravel 5.3 ดังนี้

Server Requirements

  • PHP >= 5.6.4

  • OpenSSL PHP Extension

  • PDO PHP Extension

  • Mbstring PHP Extension

  • Tokenizer PHP Extension

PHP ที่ใช้ในที่นี้คือเวอร์ชัน 7.0 เพราะฉะนั้นข้อแรกผ่าน (PHP >= 5.6.4)

[dev@centos-7 ~]$ php v

PHP 7.0.10 (cli) (built: Aug 20 2016 07:47:25) ( NTS )

Copyright (c) 1997-2016 The PHP Group

Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

ตอนนี้ในเครื่องมีแพ็คเกจ rpm ที่มีชื่อ PHP แค่ php70w-cli และ php70w-common

[dev@centos-7 ~]$ rpm qa | grep php

php70w-cli-7.0.10-1.w7.x86_64

php70w-common-7.0.10-1.w7.x86_64

ใช้คำสั่ง php ระบุออปชัน -m เพื่อแสดงชื่อโมดูล PHP ที่ใช้งานได้

[dev@centos-7 ~]$ php m

[PHP Modules]

bz2

calendar

Core

ctype

curl

date

exif

fileinfo

filter

ftp

gettext

gmp

hash

iconv

json

libxml

openssl

pcntl

pcre

Phar

readline

Reflection

session

shmop

SimpleXML

sockets

SPL

standard

tokenizer

xml

zip

zlib

[Zend Modules]

ในที่นี้จะเลือกใช้ composer ระบุออปชัน create-project ติดตั้งแพ็คเกจ laravel/laravel โดยเก็บไว้ในโฟลเดอร์ที่ชื่อว่า blog

[dev@centos-7 ~]$ composer createproject preferdist laravel/laravel blog

Installing laravel/laravel (v5.3.0)

– Installing laravel/laravel (v5.3.0)

Loading from cache

Created project in blog

> php -r “file_exists(‘.env’) || copy(‘.env.example’, ‘.env’);”

Loading composer repositories with package information

Updating dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

Problem 1

– laravel/framework v5.3.6 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

– laravel/framework v5.3.5 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

– laravel/framework v5.3.4 requires ext-mbstring * -> the requested PHP extension mbstring is missing from your system.

– Installation request for laravel/framework 5.3.* -> satisfiable by laravel/framework[v5.3.0, v5.3.1, v5.3.2, v5.3.3, v5.3.4, v5.3.5, v5.3.6].

To enable extensions, verify that they are enabled in those .ini files:

– /etc/php.ini

– /etc/php.d/bz2.ini

– /etc/php.d/calendar.ini

– /etc/php.d/ctype.ini

– /etc/php.d/curl.ini

– /etc/php.d/exif.ini

– /etc/php.d/fileinfo.ini

– /etc/php.d/ftp.ini

– /etc/php.d/gettext.ini

– /etc/php.d/gmp.ini

– /etc/php.d/iconv.ini

– /etc/php.d/json.ini

– /etc/php.d/phar.ini

– /etc/php.d/shmop.ini

– /etc/php.d/simplexml.ini

– /etc/php.d/sockets.ini

– /etc/php.d/tokenizer.ini

– /etc/php.d/xml.ini

– /etc/php.d/zip.ini

You can also run `php –ini` inside terminal to see which files are used by PHP in CLI mode.

การติดตั้งไม่สำเร็จขึ้นข้อความ requires ext-mbstring

ปัญหานี้เนื่องจาก Laravel 5.3 ต้องการใช้โมดูล PHP ชื่อ mbstring ดังนั้นต้องติดตั้งเพิ่มเติม

ใช้คำสั่ง yum ติดตั้งแพ็คเกจชื่อ php70-mbstring

[dev@centos-7 ~]$ sudo yum install php70wmbstring

Installed:

php70w-mbstring.x86_64 0:7.0.10-1.w7

Complete!

ลองรัน php ระบุออปชัน -m เพื่อดูโมดูลที่ใช้ได้หลังติดตั้งแพ็คเกจเพิ่มเติม จะมี mbstring เพิ่มขึ้นมา

[dev@centos-7 ~]$ php m

[PHP Modules]

bz2

calendar

Core

ctype

curl

date

exif

fileinfo

filter

ftp

gettext

gmp

hash

iconv

json

libxml

mbstring

openssl

pcntl

pcre

Phar

readline

Reflection

session

shmop

SimpleXML

sockets

SPL

standard

tokenizer

xml

zip

zlib

[Zend Modules]

หลังติดตั้งแพ็คเกจเพิ่มเติมแล้ว ลองใช้ composer ติดตั้ง laravel อีกครั้ง

[dev@centos-7 ~]$ composer createproject preferdist laravel/laravel blog

Installing laravel/laravel (v5.3.0)

[InvalidArgumentException]

Project directory blog/ is not empty.

create-project [-s|–stability STABILITY] [–prefer-source] [–prefer-dist] [–repository REPOSITORY] [–repository-url REPOSITORY-URL] [–dev] [–no-dev] [–no-custom-installers] [–no-scripts] [–no-progress] [–no-secure-http] [–keep-vcs] [–no-install] [–ignore-platform-reqs] [–] [<package>] [<directory>] [<version>]

ต้องลบโฟลเดอร์ที่เพิ่งถูกสร้างออกไปก่อน แล้วรัน composer ติดตั้งใหม่อีกครั้ง

[dev@centos-7 ~]$ ls l

total 4

drwxrwxr-x. 11 dev dev 4096 Sep 2 17:23 blog

[dev@centos-7 ~]$ rm rf blog/

[dev@centos-7 ~]$ composer createproject preferdist laravel/laravel blog

Installing laravel/laravel (v5.3.0)

– Installing laravel/laravel (v5.3.0)

Loading from cache

Created project in blog

> php -r “file_exists(‘.env’) || copy(‘.env.example’, ‘.env’);”

Loading composer repositories with package information

Updating dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

Problem 1

– phpunit/phpunit 5.5.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.

– phpunit/phpunit 5.5.3 requires ext-dom * -> the requested PHP extension dom is missing from your system.

– phpunit/phpunit 5.5.2 requires ext-dom * -> the requested PHP extension dom is missing from your system.

– Installation request for phpunit/phpunit ~5.0 -> satisfiable by phpunit/phpunit[5.0.0, 5.0.1, 5.0.10, 5.0.2, 5.0.3, 5.0.4, 5.0.5, 5.0.6, 5.0.7, 5.0.8, 5.0.9, 5.1.0, 5.1.1, 5.1.2, 5.1.3, 5.1.4, 5.1.5, 5.1.6, 5.1.7, 5.2.0, 5.2.1, 5.2.10, 5.2.11, 5.2.12, 5.2.2, 5.2.3, 5.2.4, 5.2.5, 5.2.6, 5.2.7, 5.2.8, 5.2.9, 5.3.0, 5.3.1, 5.3.2, 5.3.3, 5.3.4, 5.3.5, 5.4.0, 5.4.1, 5.4.2, 5.4.3, 5.4.4, 5.4.5, 5.4.6, 5.4.7, 5.4.8, 5.5.0, 5.5.1, 5.5.2, 5.5.3, 5.5.4].

To enable extensions, verify that they are enabled in those .ini files:

– /etc/php.ini

– /etc/php.d/bz2.ini

– /etc/php.d/calendar.ini

– /etc/php.d/ctype.ini

– /etc/php.d/curl.ini

– /etc/php.d/exif.ini

– /etc/php.d/fileinfo.ini

– /etc/php.d/ftp.ini

– /etc/php.d/gettext.ini

– /etc/php.d/gmp.ini

– /etc/php.d/iconv.ini

– /etc/php.d/json.ini

– /etc/php.d/mbstring.ini

– /etc/php.d/phar.ini

– /etc/php.d/shmop.ini

– /etc/php.d/simplexml.ini

– /etc/php.d/sockets.ini

– /etc/php.d/tokenizer.ini

– /etc/php.d/xml.ini

– /etc/php.d/zip.ini

You can also run `php –ini` inside terminal to see which files are used by PHP in CLI mode.

ตอนนี้จะขึ้นข้อความผิดพลาด “requires ext-dom” ต้องติดตั้งแพ็คเกจเพิ่มเติม

บน CentOS 7 โมดูล dom ของ PHP อยู่ใน rpm แพ็คเกจชื่อ php70w-xml

ใช้คำสั่ง yum ติดตั้ง

[dev@centos-7 ~]$ sudo yum install php70wxml

Installed:

php70w-xml.x86_64 0:7.0.10-1.w7

Complete!

ถ้าลองรัน php ระบุออปขัน -m จะเห็นโมดูลถูกติดตั้งเพิ่มเติมคือ dom, wddx, xmlreader, xmlwriter, xsl

[dev@centos-7 ~]$ php m

[PHP Modules]

bz2

calendar

Core

ctype

curl

date

dom

exif

fileinfo

filter

ftp

gettext

gmp

hash

iconv

json

libxml

mbstring

openssl

pcntl

pcre

Phar

readline

Reflection

session

shmop

SimpleXML

sockets

SPL

standard

tokenizer

wddx

xml

xmlreader

xmlwriter

xsl

zip

zlib

[Zend Modules]

ลบโฟลเดอร์ blog แล้วรัน composer ติดตั้ง Laravel ใหม่

[dev@centos-7 ~]$ rm rf blog/

[dev@centos-7 ~]$ composer createproject preferdist laravel/laravel blog

Installing laravel/laravel (v5.3.0)

– Installing laravel/laravel (v5.3.0)

Loading from cache

Created project in blog

> php -r “file_exists(‘.env’) || copy(‘.env.example’, ‘.env’);”

Loading composer repositories with package information

Updating dependencies (including require-dev)

– Installing vlucas/phpdotenv (v2.4.0)

Loading from cache

– Installing symfony/polyfill-mbstring (v1.2.0)

Loading from cache

– Installing symfony/var-dumper (v3.1.3)

Loading from cache

– Installing laravel/framework (v5.3.6)

Downloading: 100%

– Installing phpunit/phpunit (5.5.4)

Downloading: 100%

– Installing symfony/css-selector (v3.1.3)

Downloading: 100%

– Installing symfony/dom-crawler (v3.1.3)

Downloading: 100%

symfony/var-dumper suggests installing ext-symfony_debug ()

symfony/translation suggests installing symfony/config ()

symfony/routing suggests installing doctrine/annotations (For using the annotation loader)

symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)

symfony/routing suggests installing symfony/dependency-injection (For loading routes from a service)

symfony/routing suggests installing symfony/expression-language (For using expression matching)

symfony/event-dispatcher suggests installing symfony/dependency-injection ()

symfony/http-kernel suggests installing symfony/browser-kit ()

symfony/http-kernel suggests installing symfony/class-loader ()

symfony/http-kernel suggests installing symfony/config ()

symfony/http-kernel suggests installing symfony/dependency-injection ()

laravel/framework suggests installing league/flysystem-aws-s3-v3 (Required to use the Flysystem S3 driver (~1.0).)

laravel/framework suggests installing league/flysystem-rackspace (Required to use the Flysystem Rackspace driver (~1.0).)

laravel/framework suggests installing pda/pheanstalk (Required to use the beanstalk queue driver (~3.0).)

laravel/framework suggests installing predis/predis (Required to use the redis cache and queue drivers (~1.0).)

laravel/framework suggests installing pusher/pusher-php-server (Required to use the Pusher broadcast driver (~2.0).)

laravel/framework suggests installing symfony/psr-http-message-bridge (Required to psr7 bridging features (0.2.*).)

sebastian/global-state suggests installing ext-uopz (*)

phpunit/phpunit-mock-objects suggests installing ext-soap (*)

phpunit/php-code-coverage suggests installing ext-xdebug (>=2.4.0)

phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)

Writing lock file

Generating autoload files

> Illuminate\Foundation\ComposerScripts::postUpdate

> php artisan optimize

PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Class ‘PDO’ not found in /home/dev/blog/config/database.php:16

Stack trace:

#0 /home/dev/blog/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php(60): require()

#1 /home/dev/blog/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguration.php(38): Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles(Object(Illuminate\Foundation\Application), Object(Illuminate\Config\Repository))

#2 /home/dev/blog/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(203): Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap(Object(Illuminate\Foundation\Application))

#3 /home/dev/blog/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(262): Illuminate\Foundation\Application->bootstrapWith(Array)

#4 /home/dev/blog/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(114): Illuminate\Foundation\Console\Kernel->bootstrap()

#5 /home/dev/blog/ in /home/dev/blog/config/database.php on line 16

Script php artisan optimize handling the post-update-cmd event returned with error code 255

ยังติดตั้งไม่สำเร็จ ขึ้นข้อความ “PHP Fatal error … Class ‘PDO’ not found …”

ต้องติดตั้ง PHP โมดูลเพิ่มเติม ชื่อ php70w-pdo

[dev@centos-7 ~]$ sudo yum install php70wpdo

Installed:

php70w-pdo.x86_64 0:7.0.10-1.w7

Complete!

โมดูล PHP ที่ถูกติดตั้งเพิ่มเติมคือ PDO, pdo_sqlite, sqlite3

ลบโฟลเดอร์ blog แล้วรัน composer ใหม่

[dev@centos-7 ~]$ rm -rf blog/

[dev@centos-7 ~]$ composer create-project –prefer-dist laravel/laravel blog

Installing laravel/laravel (v5.3.0)

– Installing laravel/laravel (v5.3.0)

Loading from cache

Created project in blog

> php -r “file_exists(‘.env’) || copy(‘.env.example’, ‘.env’);”

Loading composer repositories with package information

Updating dependencies (including require-dev)

– Installing vlucas/phpdotenv (v2.4.0)

Loading from cache

– Installing symfony/polyfill-mbstring (v1.2.0)

Loading from cache

– Installing symfony/var-dumper (v3.1.3)

Loading from cache

– Installing laravel/framework (v5.3.6)

Loading from cache

– Installing phpunit/phpunit (5.5.4)

Loading from cache

– Installing symfony/css-selector (v3.1.3)

Loading from cache

– Installing symfony/dom-crawler (v3.1.3)

Loading from cache

symfony/var-dumper suggests installing ext-symfony_debug ()

symfony/translation suggests installing symfony/config ()

symfony/routing suggests installing doctrine/annotations (For using the annotation loader)

symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)

symfony/routing suggests installing symfony/dependency-injection (For loading routes from a service)

laravel/framework suggests installing league/flysystem-aws-s3-v3 (Required to use the Flysystem S3 driver (~1.0).)

laravel/framework suggests installing league/flysystem-rackspace (Required to use the Flysystem Rackspace driver (~1.0).)

laravel/framework suggests installing pda/pheanstalk (Required to use the beanstalk queue driver (~3.0).)

laravel/framework suggests installing predis/predis (Required to use the redis cache and queue drivers (~1.0).)

laravel/framework suggests installing pusher/pusher-php-server (Required to use the Pusher broadcast driver (~2.0).)

laravel/framework suggests installing symfony/psr-http-message-bridge (Required to psr7 bridging features (0.2.*).)

sebastian/global-state suggests installing ext-uopz (*)

phpunit/phpunit-mock-objects suggests installing ext-soap (*)

phpunit/php-code-coverage suggests installing ext-xdebug (>=2.4.0)

phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)

Writing lock file

Generating autoload files

> Illuminate\Foundation\ComposerScripts::postUpdate

> php artisan optimize

Generating optimized class loader

> php artisan key:generate

Application key [base64:BDe4eFfvlQON+ORBYHlfG6wostHsQ2thFSidUWJDf0c=] set successfully.

ถึงตอนนี้ เราได้ติดตั้ง Laravel 5.3 สำเร็จ เรียบร้อยแล้ว

แนะนำให้ติดตั้งแพ็คเกจ php70w-mysql เพิ่มเติม เพื่อให้ PHP เชื่อมต่อเข้ากับฐานข้อมูล MySQL ได้

[dev@centos-7 ~]$ sudo yum install php70wmysql

สรุปว่าตอนนี้บนเครื่อง CentOS 7 ที่เราใช้ทดสอบติดตั้ง Laravel 5.3 นั้น มีแพ็คเกจ rpm ชื่อ php ติดตั้งดังนี้

[dev@centos-7 ~]$ rpm qa | grep php

php70w-cli-7.0.10-1.w7.x86_64

php70w-common-7.0.10-1.w7.x86_64

php70w-mbstring-7.0.10-1.w7.x86_64

php70w-mysql-7.0.10-1.w7.x86_64

php70w-pdo-7.0.10-1.w7.x86_64

php70w-xml-7.0.10-1.w7.x86_64

รายชื่อโมดูล PHP ที่สามารถเรียกใช้ได้

[dev@centos-7 ~]$ php m

[PHP Modules]

bz2

calendar

Core

ctype

curl

date

dom

exif

fileinfo

filter

ftp

gettext

gmp

hash

iconv

json

libxml

mbstring

mysqli

openssl

pcntl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

readline

Reflection

session

shmop

SimpleXML

sockets

SPL

sqlite3

standard

tokenizer

wddx

xml

xmlreader

xmlwriter

xsl

zip

zlib

[Zend Modules]

cd เข้าไปใน blog ลองดูไฟล์ที่ได้จากการติดตั้ง Laravel 5.3

[dev@centos-7 ~]$ cd blog/

[dev@centos-7 blog]$ ls l

total 160

drwxrwxr-x. 6 dev dev     79 Sep 2 17:41 app

-rw-rw-r–. 1 dev dev   1646 Sep 2 17:41 artisan

drwxrwxr-x. 3 dev dev     51 Sep 2 17:41 bootstrap

-rw-rw-r–. 1 dev dev   1283 Sep 2 17:41 composer.json

-rw-rw-r–. 1 dev dev 123589 Sep 2 17:41 composer.lock

drwxrwxr-x. 2 dev dev   4096 Sep 2 17:41 config

drwxrwxr-x. 5 dev dev     68 Sep 2 17:41 database

-rw-rw-r–. 1 dev dev    556 Sep 2 17:41 gulpfile.js

-rw-rw-r–. 1 dev dev    400 Sep 2 17:41 package.json

-rw-rw-r–. 1 dev dev    930 Sep 2 17:41 phpunit.xml

drwxrwxr-x. 4 dev dev     92 Sep 2 17:41 public

-rw-rw-r–. 1 dev dev   1918 Sep 2 17:41 readme.md

drwxrwxr-x. 5 dev dev     42 Sep 2 17:41 resources

drwxrwxr-x. 2 dev dev     52 Sep 2 17:41 routes

-rw-rw-r–. 1 dev dev    563 Sep 2 17:41 server.php

drwxrwxr-x. 5 dev dev     43 Sep 2 17:41 storage

drwxrwxr-x. 2 dev dev     47 Sep 2 17:41 tests

drwxrwxr-x. 31 dev dev  4096 Sep 2 17:41 vendor

ไฟล์สำคัญไฟล์หนึ่งของ Laravel คือ artisan ลองรันด้วย php จะแสดงคำสังหรือออปชัน พร้อมคำอธิบายการใช้งาน

[dev@centos-7 blog]$ php artisan

Laravel Framework version 5.3.6

Usage:

command [options] [arguments]

Options:

-h, –help            Display this help message

-q, –quiet           Do not output any message

-V, –version         Display this application version

–ansi            Force ANSI output

–no-ansi         Disable ANSI output

-n, –no-interaction  Do not ask any interactive question

–env[=ENV]       The environment the command should run under.

-v|vv|vvv, –verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:

clear-compiled       Remove the compiled class file

down                 Put the application into maintenance mode

env                  Display the current framework environment

help                 Displays help for a command

inspire

list                 Lists commands

migrate              Run the database migrations

optimize             Optimize the framework for better performance

serve                Serve the application on the PHP development server

tinker               Interact with your application

up                   Bring the application out of maintenance mode

app

app:name             Set the application namespace

auth

auth:clear-resets    Flush expired password reset tokens

cache

cache:clear          Flush the application cache

cache:table          Create a migration for the cache database table

config

config:cache         Create a cache file for faster configuration loading

config:clear         Remove the configuration cache file

db

db:seed              Seed the database with records

event

event:generate       Generate the missing events and listeners based on registration

key

key:generate         Set the application key

make

make:auth            Scaffold basic login and registration views and routes

make:command         Create a new Artisan command

make:controller      Create a new controller class

make:event           Create a new event class

make:job             Create a new job class

make:listener        Create a new event listener class

make:mail            Create a new email class

make:middleware      Create a new middleware class

make:migration       Create a new migration file

make:model           Create a new Eloquent model class

make:notification    Create a new notification class

make:policy          Create a new policy class

make:provider        Create a new service provider class

make:request         Create a new form request class

make:seeder          Create a new seeder class

make:test            Create a new test class

migrate

migrate:install      Create the migration repository

migrate:refresh      Reset and re-run all migrations

migrate:reset        Rollback all database migrations

migrate:rollback     Rollback the last database migration

migrate:status       Show the status of each migration

notifications

notifications:table  Create a migration for the notifications table

queue

queue:failed         List all of the failed queue jobs

queue:failed-table   Create a migration for the failed queue jobs database table

queue:flush          Flush all of the failed queue jobs

queue:forget         Delete a failed queue job

queue:listen         Listen to a given queue

queue:restart        Restart queue worker daemons after their current job

queue:retry          Retry a failed queue job

queue:table          Create a migration for the queue jobs database table

queue:work           Process the next job on a queue

route

route:cache          Create a route cache file for faster route registration

route:clear          Remove the route cache file

route:list           List all registered routes

schedule

schedule:run         Run the scheduled commands

session

session:table        Create a migration for the session database table

storage

storage:link         Create a symbolic link from “public/storage” to “storage/app/public”

vendor

vendor:publish       Publish any publishable assets from vendor packages

view

view:clear           Clear all compiled view files

คำสั่ง (command) แรกที่แนะนำคือ serve ใช้รันเป็นเว็บเซิร์ฟเวอร์ตอนพัฒนาเว็บไซต์ด้วย Laravel ได้เลย ไม่จำเป็นต้องติดตั้งเว็บเซิร์ฟเวอร์ เช่น Apache หรือ Nginx แต่อย่างใด

วิธีการรันคือ php artisan serve

[dev@centos-7 blog]$ php artisan serve

Laravel development server started on http://localhost:8000/

หากไม่ระบุออปชันเพิ่มเติมของ คำสั่ง serve จะรันเป็นเว็บเซิร์ฟเวอร์เรียกได้เฉพาะภายเครื่องเซิรฟ์เวอร์เอง (localhost)

กด [Ctrl]+[c] เพื่อยกเลิกการรัน php artisan serve

ต้องระบุออปชัน –host เพื่อระบุ IP ของเซิร์ฟเวอร์ เพื่อให้เครื่องอื่นๆ สามารถมาเรียกใช้งานเว็บได้

[dev@centos-7 blog]$ php artisan serve host 192.168.56.107

Laravel development server started on http://192.168.56.107:8000/

ลองเปิดด้วย browser ระบุ url ตาม IP และเลขพอร์ต

ตัวอย่างข้อความที่แสดงขึ้นมา เวลามีเครื่องอื่น (client) มาเรียกใช้

[dev@centos-7 blog]$ php artisan serve host 192.168.56.107

Laravel development server started on http://192.168.56.107:8000/

[Fri Sep 2 17:51:14 2016] 192.168.56.1:49881 [200]: /favicon.ico

กด [Ctrl]+[c] เพื่อยกเลิกการรัน

 

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *