Heim  >  Artikel  >  Backend-Entwicklung  >  vagrant系列课程(三):vagrant搭建的php7环境

vagrant系列课程(三):vagrant搭建的php7环境

WBOY
WBOYOriginal
2016-06-13 12:29:221000Durchsuche

vagrant系列教程(三):vagrant搭建的php7环境

前面已经把vagrant的基础知识已经基本过了一遍 了,相信只要按着教程来,你已经搭建好了自己的基础环境。接下来说一说如何搭建php7的开发环境。

申明一下,这里使用的box,就是前面演示的centos7
地址奉上:
https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box

安装nginx

首先需要跟新一些ngin的相关源。

<code class="language-shell hljs avrasm">$ rpm -Uvh http://nginx<span class="hljs-preprocessor">.org</span>/packages/centos/<span class="hljs-number">7</span>/noarch/RPMS/nginx-release-centos-<span class="hljs-number">7</span>-<span class="hljs-number">0.</span>el7<span class="hljs-preprocessor">.ngx</span><span class="hljs-preprocessor">.noarch</span><span class="hljs-preprocessor">.rpm</span>// 执行安装过程$ yum install nginx </code>

看到以下界面时,请稍等,如果需要输入的地方,请直接按 y 然后回车。
这里写图片描述

启动nginx并设置为开机启动

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>systemctl start nginx <span class="hljs-variable">$ </span>systemctl enable nginx</code>

安装epel与remi源

安装epel,epel是Fedora小组维护的一个软件仓库项目,为RHEL/CentOS提供他们默认不提供的软件包。安装时一定需要注意一下自己系统的版本额。

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>rpm -ivh <span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/mirrors.opencas.cn/epel</span><span class="hljs-regexp">/7/x</span>86_64/e/epel-release-<span class="hljs-number">7</span>-<span class="hljs-number">5</span>.noarch.rpm</code>

remi源种包含最新的php相关信息,如:php7、mysql等,因此为了便捷获取php7的最新信息,也需要安装一下这个源。

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>rpm -ivh <span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/rpms.famillecollet.com/enterprise</span><span class="hljs-regexp">/remi-release-7.rpm</span></code>

iptables防火墙

因为以前经常使用iptables,对centos7自带的firewalld防火墙不熟悉,因此我就将关闭centos7自带的firewalld,启用自己熟悉的iptables吧。

首先,关闭自带的firewalld防火墙

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>systemctl stop firewalld.service <span class="hljs-variable">$ </span>systemctl disable firewalld.service <span class="hljs-comment">#防止开机启动</span></code>

安装iptables

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>yum install iptables-services</code>

安装进程如下图所示
这里写图片描述

启动iptables防火墙

<code class="language-shell hljs avrasm">systemctl start iptables<span class="hljs-preprocessor">.service</span> systemctl enable iptables<span class="hljs-preprocessor">.service</span> <span class="hljs-preprocessor">#开机自动启动</span></code>

编辑防火墙配置文件

为了我们在自己的主机上能够顺利访问,需要开启以下端口,
vim /etc/sysconfig/iptables
编辑防火墙,设置80(nginx) 3306(mysql/mariadb) 6379(redis)端口,外网可访问
这里写图片描述

PHP7.0的安装

查看remi源中可安装的php信息

<code class="language-shell hljs brainfuck"><span class="hljs-comment">$</span> <span class="hljs-comment">yum</span> <span class="hljs-comment">list</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">enablerepo=remi</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">enablerepo=remi</span><span class="hljs-literal">-</span><span class="hljs-comment">php70</span> <span class="hljs-comment">|</span> <span class="hljs-comment">grep</span> <span class="hljs-comment">php70</span></code>

该列表会列出所有可以安装的php模块信息,从中安装自己需要的模块,下面安装模块,是我自己的一个模块选择情况。其中有一部分是必须的,有一部分是可选的。比如php-fpm就是必须的,如果你用的是nginx的话。

<code class="language-shell hljs lasso">$ yum install <span class="hljs-subst">--</span>enablerepo<span class="hljs-subst">=</span>remi <span class="hljs-subst">--</span>enablerepo<span class="hljs-subst">=</span>remi<span class="hljs-attribute">-php70</span> php php<span class="hljs-attribute">-opcache</span> php<span class="hljs-attribute">-pecl</span><span class="hljs-attribute">-apcu</span> php<span class="hljs-attribute">-devel</span> php<span class="hljs-attribute">-mbstring</span> php<span class="hljs-attribute">-mcrypt</span> php<span class="hljs-attribute">-mysqlnd</span> php<span class="hljs-attribute">-pecl</span><span class="hljs-attribute">-xdebug</span> php<span class="hljs-attribute">-pdo</span> php<span class="hljs-attribute">-pear</span> php<span class="hljs-attribute">-fpm</span> php<span class="hljs-attribute">-cli</span> php<span class="hljs-attribute">-xml</span> php<span class="hljs-attribute">-bcmath</span> php<span class="hljs-attribute">-process</span> php<span class="hljs-attribute">-gd</span> php<span class="hljs-attribute">-common</span> php<span class="hljs-attribute">-json</span> php<span class="hljs-attribute">-imap</span> php<span class="hljs-attribute">-pecl</span><span class="hljs-attribute">-redis</span> php<span class="hljs-attribute">-pecl</span><span class="hljs-attribute">-memcached</span> php<span class="hljs-attribute">-pecl</span><span class="hljs-attribute">-mongodb</span></code>

安装完成后,输入 php -v 可以查看当前安装的php版本信息。
这里写图片描述

启动php-fpm,因为nginx需要通过它来解析php程序

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>systemctl start php-fpm<span class="hljs-variable">$ </span>systemctl enable php-fpm <span class="hljs-comment">#设置开机自启动</span></code>

配置nginx可以访问php

进入nginx的文件配置中心,

<code class="language-shell hljs avrasm">$ cd /etc/nginx/conf<span class="hljs-preprocessor">.d</span>/<span class="hljs-preprocessor"># 复制默认的配置文件 </span>$ <span class="hljs-keyword">cp</span> default<span class="hljs-preprocessor">.conf</span> php<span class="hljs-preprocessor">.conf</span> </code>

首先先通过vim编辑default文件。将监听端口改为8080, 因为后面我们自己的php.conf会用到80端口。
这里写图片描述

现在来编辑复制的php.conf文件,可以直接复制以下内容,至于配置的含义,后面再开一篇文章来单独讲解吧。

<code class="language-config hljs axapta"><span class="hljs-keyword">server</span> {    listen       <span class="hljs-number">80</span>;    server_name  localhost;    charset utf-<span class="hljs-number">8</span>;    root /vagrant/www;<span class="hljs-preprocessor"># 自己的项目目录,也就是php项目所在目录</span>    location / {        <span class="hljs-preprocessor"># 请注意,一定要加index.php这项</span>        <span class="hljs-keyword">index</span>  <span class="hljs-keyword">index</span>.php <span class="hljs-keyword">index</span>.html <span class="hljs-keyword">index</span>.htm;    }    error_page   <span class="hljs-number">500</span> <span class="hljs-number">502</span> <span class="hljs-number">503</span> <span class="hljs-number">504</span>  /<span class="hljs-number">50</span>x.html;    location = /<span class="hljs-number">50</span>x.html {        root   /usr/share/nginx/html;    }    location ~ \.php$ {        fastcgi_pass   <span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">9000</span>;        fastcgi_index  <span class="hljs-keyword">index</span>.php;        <span class="hljs-preprocessor"># 注意此处变量的不同</span>        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    }}</code>

修改完文件后,必须要重新启动nginx,才会使当前的配置生效。

<code class="language-shell hljs ruby"><span class="hljs-variable">$ </span>systemctl reload nginx</code>

测试访问

在/vagrant/www下新建一个文件index.php

<code class="language-php hljs "><span class="hljs-preprocessor"><?php</span>    phpinfo();</code>

在浏览器中打开,访问对应的ip,可以看到输出的php信息
这里写图片描述

注:如果在使用过程中,有新增加的php模块,需要重新启动php-fpm

systemctl reload php-fpm

Mariadb的安装

这里很多同学可能第一次听说mariadb,他呢是mysql的一个重要分子,或者可以理解为mysql的替代品,自从mysql被控制后,更新速度已经慢太多了。两者的用法基本没有区别,实际中有哪些坑,大家可以自己去踩一踩。哈哈,别说我不负责任。

<code class="language-shell hljs ruby"><span class="hljs-comment"># 安装</span><span class="hljs-variable">$ </span>yum install mariadb-server<span class="hljs-comment"># 启动服务</span><span class="hljs-variable">$ </span>systemctl start mariadb<span class="hljs-comment"># 开机启动</span><span class="hljs-variable">$ </span>systemctl enable mariadb</code>

MariaDB的安全配置

MariaDB默认root密码为空,我们需要设置一下,执行脚本:

<code class="language-shell hljs bash">$ <span class="hljs-built_in">sudo</span> mysql_secure_installation</code>

这个脚本会经过一些列的交互问答来进行MariaDB的安全设置。

首先提示输入当前的root密码:

Enter current password for root (enter for none):
初始root密码为空,我们直接敲回车进行下一步。

Set root password? [Y/n]
设置root密码,默认选项为Yes,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。

Remove anonymous users? [Y/n]
是否移除匿名用户,默认选项为Yes,建议按默认设置,回车继续。

Disallow root login remotely? [Y/n]
是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按默认设置,回车继续。 如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n。

Remove test database and access to it? [Y/n]
是否删除测试用的数据库和权限? 建议按照默认设置,回车继续。

Reload privilege tables now? [Y/n]
是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。

完成后你会看到Success!的提示,MariaDB的安全设置已经完成。我们可以使用以下命令登录MariaDB:

<code class="language-shell hljs lasso">$ mysql <span class="hljs-attribute">-uroot</span> <span class="hljs-attribute">-p</span></code>

按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。
这里写图片描述

最后我们将MariaDB设置为开机启动。

<code class="language-shell hljs bash">$ <span class="hljs-built_in">sudo</span> systemctl enable mariadb</code>

让外网可以进行链接

<code class="language-mysql hljs lasso">mysql<span class="hljs-subst">></span> grant <span class="hljs-literal">all</span> <span class="hljs-keyword">on</span> <span class="hljs-subst">*</span><span class="hljs-built_in">.</span><span class="hljs-subst">*</span> <span class="hljs-keyword">to</span> <span class="hljs-string">'root'</span>@<span class="hljs-string">'%'</span> identified <span class="hljs-keyword">by</span> <span class="hljs-string">'root'</span>;mysql<span class="hljs-subst">></span> flush privileges;</code>

安装composer

composer的大名,我就不想介绍了,如果你是一个phper,没用过,我也就不怪你,毕竟但是他现在才刚过1.0版,但是如果听都没听过,请面壁去……

安装说明

<code class="language-php hljs ">$ php -r “readfile(‘https:<span class="hljs-comment">//getcomposer.org/installer‘);” > composer-setup.php</span>$ php composer-setup.php$ php -r “unlink(‘composer-setup.php’);”</code>

上述 3 条命令的作用依次是:

  1. 下载安装脚本(composer-setup.php)到当前目录。
  2. 执行安装过程。
  3. 删除安装脚本 – composer-setup.php 。

全局安装composer

全局安装是将 Composer 安装到系统环境变量 PATH 所包含的路径下面,然后就能够在命令行窗口中直接执行 composer 命令了。

Mac 或 Linux 系统:打开命令行窗口并执行如下命令将前面下载的 composer.phar 文件移动到 /usr/local/bin/ 目录下面:

<code class="language-php hljs ">$ sudo mv composer.phar /usr/local/bin/composer</code>

然后执行:
composer -v
这里写图片描述
由于composer的包都在国外,这里设置一下composer的配置,让其每次运行时,都使用国内的包

<code class="language-php hljs ">$ composer config -g repo.packagist composer https:<span class="hljs-comment">//packagist.phpcomposer.com</span></code>

查看composer的配置文件
vim /root/.composer/config.json
这里写图片描述
看到以上内容,表示配置成功!


至此,基本的php7环境搭建已经完成了。然后呢,下一次说一说用vagrant搭建redis吧。当然我不会仅仅只说redis的搭建额。到时候看啊吧!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn