vagrant系列课程(三):vagrant搭建的php7环境
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="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="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="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="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="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="shell hljs ruby"><span class="hljs-variable">$ </span>yum install iptables-services</code>
安装进程如下图所示
启动iptables防火墙
<code class="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="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="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="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="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="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="shell hljs ruby"><span class="hljs-variable">$ </span>systemctl reload nginx</code>
测试访问
在/vagrant/www下新建一个文件index.php
<code class="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="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="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="shell hljs lasso">$ mysql <span class="hljs-attribute">-uroot</span> <span class="hljs-attribute">-p</span></code>
按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。
最后我们将MariaDB设置为开机启动。
<code class="shell hljs bash">$ <span class="hljs-built_in">sudo</span> systemctl enable mariadb</code>
让外网可以进行链接
<code class="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="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 条命令的作用依次是:
- 下载安装脚本(composer-setup.php)到当前目录。
- 执行安装过程。
- 删除安装脚本 – composer-setup.php 。
全局安装composer
全局安装是将 Composer 安装到系统环境变量 PATH 所包含的路径下面,然后就能够在命令行窗口中直接执行 composer 命令了。
Mac 或 Linux 系统:打开命令行窗口并执行如下命令将前面下载的 composer.phar 文件移动到 /usr/local/bin/ 目录下面:
<code class="php hljs ">$ sudo mv composer.phar /usr/local/bin/composer</code>
然后执行: composer -v
由于composer的包都在国外,这里设置一下composer的配置,让其每次运行时,都使用国内的包
<code class="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的搭建额。到时候看啊吧!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在 Linux 中启动 Nginx 的步骤:检查 Nginx 是否已安装。使用 systemctl start nginx 启动 Nginx 服务。使用 systemctl enable nginx 启用在系统启动时自动启动 Nginx。使用 systemctl status nginx 验证启动是否成功。在 Web 浏览器中访问 http://localhost 查看默认欢迎页面。

如何在 Windows 中配置 Nginx?安装 Nginx 并创建虚拟主机配置。修改主配置文件并包含虚拟主机配置。启动或重新加载 Nginx。测试配置并查看网站。选择性启用 SSL 并配置 SSL 证书。选择性设置防火墙允许 80 和 443 端口流量。

启动 Nginx 服务器需要按照不同操作系统采取不同的步骤:Linux/Unix 系统:安装 Nginx 软件包(例如使用 apt-get 或 yum)。使用 systemctl 启动 Nginx 服务(例如 sudo systemctl start nginx)。Windows 系统:下载并安装 Windows 二进制文件。使用 nginx.exe 可执行文件启动 Nginx(例如 nginx.exe -c conf\nginx.conf)。无论使用哪种操作系统,您都可以通过访问服务器 IP

确认 Nginx 是否启动的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 检查端口 80 是否开放;3. 查看系统日志中 Nginx 启动消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

问题的答案:304 Not Modified 错误表示浏览器已缓存客户端请求的最新资源版本。解决方案:1. 清除浏览器缓存;2. 禁用浏览器缓存;3. 配置 Nginx 允许客户端缓存;4. 检查文件权限;5. 检查文件哈希;6. 禁用 CDN 或反向代理缓存;7. 重启 Nginx。

在 Linux 中,使用以下命令检查 Nginx 是否已启动:systemctl status nginx根据命令输出进行判断:如果显示 "Active: active (running)",则 Nginx 已启动。如果显示 "Active: inactive (dead)",则 Nginx 已停止。

服务器无权访问所请求的资源,导致 nginx 403 错误。解决方法包括:检查文件权限。检查 .htaccess 配置。检查 nginx 配置。配置 SELinux 权限。检查防火墙规则。排除其他原因,如浏览器问题、服务器故障或其他可能的错误。

如何解决 Nginx 403 Forbidden 错误?检查文件或目录权限;2. 检查 .htaccess 文件;3. 检查 Nginx 配置文件;4. 重启 Nginx。其他可能原因还包括防火墙规则、SELinux 设置或应用程序问题。
