Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

时间:2022-01-17 17:10:02

下载软件包

PHP:  http://windows.php.net/downloads/releases/php-5.6.20-Win32-VC11-x64.zip

Apache httpd:  http://www.apachehaus.com/downloads/httpd-2.4.18-x64-vc11-r3.zip

Visual C++ 2012 x64 Redistributable:  https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x64.exe

Composer:  https://getcomposer.org/Composer-Setup.exe

MySQL:  http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip

安装过程

  • 安装vcredist_x64.exe
  • 解压apache httpd,php,mysql到D:\wamp
  • 创建默认站点目录D:\wamp\www
  • 创建数据库data目录D:\wamp\mydata

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

apache httpd设置

添加环境变量

将httpd安装目录下的bin路径添加到Path变量,这里是D:\wamp\Apache24\bin

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

修改httpd配置文件 D:\wamp\Apache24\conf\httpd.conf

修改SRVROOT指向到安装目录

Define SRVROOT "D:/wamp/Apache24"

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

PHP集成

# PHP 5
LoadModule php5_module "D:/wamp/php/php5apache2_4.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "D:/wamp/php"

添加index.php为默认文档

DirectoryIndex index.php index.html

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

多端口站点配置

添加多端口

Listen 8081

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

添加多端口站点

# Virtual Hosts
<VirtualHost *:8081>
    DocumentRoot "D:/wamp/www/site1"
    ServerName localhost
    ErrorLog "logs/site1-error.log"
    CustomLog "logs/site1-access.log" common
    <Directory "D:/wamp/www/site1">
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

多域名站点配置

添加域名映射到C:\Windows\System32\drivers\etc\hosts文件中

127.0.0.1      site1.example.com

添加多域名站点

<VirtualHost *:80>
    DocumentRoot "D:/wamp/www/site1"
    ServerName site1.example.com
    ErrorLog "logs/site1-error.log"
    CustomLog "logs/site1-access.log" common
    <Directory "D:/wamp/www/site1">
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

创建站点目录D:/wamp/www/site1

创建首页D:/wamp/www/site1/index.php

<?php
  phpinfo();
?>

把Apache httpd安装成后台服务

httpd -k install

设置开机自启动ApacheMonitor程序

将D:/wamp/Apache24\bin\ApacheMonitor.exe拷贝到C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp中

启动apache httpd服务

httpd -k start

php设置

添加环境变量

将php安装路径添加到Path变量,这里是D:\wamp\php

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

进入PHP安装目录,找到 php.ini-development(生产环境用php.ini-production) 文件并复制一份到当前目录,重命名为 php.ini

修改php配置文件php.ini

修改ext文件夹的路径(extension_dir="D:/wamp/php/ext")

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

要在 php.ini 中启用某扩展库,需要去掉该行 extension=php_*.dll 前的注释符号,这里需要启用 php_mbstring,php_openssl 扩展库

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

Windows 下安装扩展库: http://www.golaravel.com/php/install.windows.extensions.html#install.windows.extensions.overview

开通Windows防火墙

netsh advfirewall firewall add rule name=WebServer dir=in action=allow protocol=TCP localport=80,443,8080-8090

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

测试站点

访问站点 http://localhost:8081

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

安装MySQL

进入mysql安装目录,找到 my-default.ini,重命名为 my.ini

修改mysql配置文件my.ini

 

basedir = D:/wamp/mysql
datadir = D:/wamp/mydata
port = 3306

 

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

打开命令行窗口,执行以下命令来安装并启动mysql

cd /d D:\wamp\mysql
bin\mysqld --initialize-insecure
bin\mysqld --install
NET START MySQL

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

输入以下命令执行mysql的安全设置

bin\mysql_secure_installation

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

将D:\wamp\mysql\bin添加到PATH环境变量

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

之后就可以在命令行窗口直接输入以下命令进入mysql控制台

mysql -u root -p

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

安装Composer

方式一:使用Composer.exe安装

 Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

方式二:手动配置

  • 下载composer.phar(https://getcomposer.org/composer.phar)到任意目录,这里是D:\wamp\composer
  • 在该目录下新建一个 composer.bat 文件,并将下列代码保存到此文件中。
@echo OFF
:: in case DelayedExpansion is on and a path contains ! 
setlocal DISABLEDELAYEDEXPANSION
php "%~dp0composer.phar" %*
  • 将该路径添加到PATH环境变量

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

Composer中文站点:http://www.phpcomposer.com/

使用Composer

要开始在你的项目中使用 Composer,你只需要一个 composer.json 文件。该文件包含了项目的依赖和其它的一些元数据。

安装依赖包

composer install -vvv

在安装依赖后,Composer 将把安装时确切的版本号列表写入 composer.lock 文件。这将锁定改项目的特定版本。

Composer基本用法:http://docs.phpcomposer.com/01-basic-usage.html

更新Composer

composer selfupdate

安装Laravel

使用 Composer 下载 Laravel 安装包

composer global require "laravel/installer"

添加%USERPROFILE%\AppData\Roaming\Composer\vendor\bin到用户的PATH环境变量

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

创建一个Laravel站点

使用 laravel new 命令在你指定的目录中建立一份全新安装的Laravel应用。例如:laravel new blog 命令会在当前目录下建立一个名为 blog 的目录。

cd /d D:\wamp\www\
laravel new blog

进入blog目录,运行composer install 来安装依赖。

cd blog
composer install

将blog目录中 .env.example文件改名为 .env

rename .env.example .env

重新生成32个字符的key

php artisan key:generate

修改httpd配置文件 D:\wamp\Apache24\conf\httpd.conf

添加监听端口

Listen 8082

添加多端口站点配置

<VirtualHost *:8082>
    DocumentRoot "D:/wamp/www/blog/public"
    ServerName localhost
    ErrorLog "logs/blog-error.log"
    CustomLog "logs/blog-access.log" common
    <Directory "D:/wamp/www/blog/public">
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重启httpd服务

httpd -k restart

测试站点

访问站点 http://localhost:8082

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

Laravel安装方法:http://www.golaravel.com/laravel/docs/5.1/#installation

基本配置

安装 Laravel 之后,可能需要你配置一下目录权限。web 服务器需要拥有 storage 目录下的所有目录和 bootstrap/cache 目录的写权限。

Laravel环境配置: http://www.golaravel.com/laravel/docs/5.1/installation/#environment-configuration

安装MySQL

双击mysql安装程序,选择"Developer Default"

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

跳过安装这些需求包

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

开始安装

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

选择“Development Machine”,端口默认3306

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

设置root帐号的密码

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

配置Windows Service,开机自启动

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

执行服务器配置

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

连接Mysql

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

配置samples和examples

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

点击Finish,安装完成。

备注

查看80端口是否被占用,命令行下输入:

netstat -aon|findstr "80" 

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

如果看到上图的结果,说明80端口已被使用,需要先关闭相关软件,或者修改apache默认的监听端口

打开apache目录下的conf/httpd.conf  搜索   "Listen 80" ,修改为Listen 88 , 保存之后再重新启动apache

composer install 背后到底是怎么运作的?

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

内网服务器需要开通以下防火墙

Domain       Ports      Type
getcomposer.org    80, 443    TCP
packagist.org    80, 443    TCP
github.com    80, 443    TCP

Laravel项目如果出现Whoops, looks like something went wrong.错误

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

开启Debug模式,修改环境配置文件.env,将APP_DEBUG设置为true。

APP_DEBUG=true

刷新页面,这时页面如下显示详细错误。

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

这是因为config/app.php中默认的cipher为"AES-256-CBC", 这需要key的字串的长度是 32 个字符,但是默认的key为"SomeRandomString",长度是16个字符。

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

进入blog目录,运行 php artisan key:generate 命令重新生成32个字符的key

cd /d D:\wamp\www\blog\
php artisan key:generate

Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

 

这时重新刷新页面,错误消失。

laravel项目源码

Laravel书籍推荐

Getting Started with Laravel 4(基础,建议先看这本)
Laravel Application Development Blueprints(10个小项目)
Laravel 4 Cookbook(Laravel的相关扩展知识)
Learning Laravel 4 Application Development(一个完整项目,适合有基础的看,有些地方有错误)