This article introduces how to build a php7 development environment with vagrant. It covers the installation and configuration of nginx, the installation and configuration of composer, php7 installation and module installation, and the configuration of the firewall in centos7.
I have basically gone through the basic knowledge of vagrant before. I believe that as long as you follow the tutorial, you will have set up your own basic environment. Next, let’s talk about how to set up a development environment for php7.
Let me state that the box used here is the centos7 demonstrated earlier
The address is provided:
https://github.com/tommy-muehle/puppet-vagrant-boxes/ releases/download/1.1.0/centos-7.0-x86_64.box
First you need to update some ngin-related sources.
$ rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm // 执行安装过程 $ yum install nginx
When you see the following interface, please wait. If you need to enter something, please press y and press Enter.
$ systemctl start nginx $ systemctl enable nginx
Install epel. epel is a software warehouse project maintained by the Fedora team, which provides RHEL/CentOS with software packages that they do not provide by default. When installing, you must pay attention to the version of your system.
$ rpm -ivh http://mirrors.opencas.cn/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
Modified on 2016-10-22:
The source posted above recently cannot be used. Please find the corresponding version here
http://dl.fedoraproject.org/pub/
If you can’t pass the wall, please use the domestic mirror
http://mirrors.sohu.com/fedora-epel/7/x86_64/e/epel-release-7-8.noarch.rpm
remi source contains the latest php related information, Such as: php7, mysql, etc. Therefore, in order to easily obtain the latest information of php7, you also need to install this source.
$ rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Because I often use iptables before, I am not familiar with the firewalld firewall that comes with centos7, so I will turn off the firewalld that comes with centos7 and enable it myself. Be familiar with iptables.
First, turn off the built-in firewalld firewall
$ systemctl stop firewalld.service $ systemctl disable firewalld.service #防止开机启动
$ yum install iptables-services
The installation process is as shown in the figure below
Start iptables firewall
systemctl start iptables.service systemctl enable iptables.service #开机自动启动
In order for us to have smooth access on our own host, we need to open the following ports,
vim /etc/sysconfig/iptables
Edit the firewall, set 80 (nginx) 3306 (mysql/mariadb) 6379 (redis) port, accessible from the external network
View the installable php information in the remi source
$ yum list --enablerepo=remi --enablerepo=remi-php70 | grep php70
The list will list all the php that can be installed Module information, from which you can install the modules you need. Installing the modules below is my own module selection. Some of them are required and some are optional. For example, php-fpm is necessary if you are using nginx.
$ yum install --enablerepo=remi --enablerepo=remi-php70 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-pecl-xdebug php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common php-json php-imap php-pecl-redis php-pecl-memcached php-pecl-mongodb
After the installation is complete, enter php -v to view the currently installed php version information.
Start php-fpm, because nginx needs it to parse the php program
$ systemctl start php-fpm $ systemctl enable php-fpm #设置开机自启动
Enter the nginx file configuration center,
$ cd /etc/nginx/conf.d/ # 复制默认的配置文件 $ cp default.conf php.conf
First edit the default file through vim. Change the listening port to 8080, because our own php.conf will use port 80 later.
Now let’s edit the copied php.conf file. You can directly copy the following content. As for the meaning of configuration, I will open an article to explain it separately later.
server { listen 80; server_name localhost; charset utf-8; root /vagrant/www;# 自己的项目目录,也就是php项目所在目录 location / { # 请注意,一定要加index.php这项 index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # 注意此处变量的不同 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
After modifying the file, nginx must be restarted for the current configuration to take effect.
$ systemctl reload nginx
Create a new file index.php under /vagrant/www
<?php phpinfo();
Open it in the browser and access the corresponding ip , you can see the output php information
Note: If there are newly added php modules during use, you need to restart php-fpm
systemctl reload php-fpm
这里很多同学可能第一次听说mariadb,他呢是mysql的一个重要分子,或者可以理解为mysql的替代品,自从mysql被控制后,更新速度已经慢太多了。两者的用法基本没有区别,实际中有哪些坑,大家可以自己去踩一踩。哈哈,别说我不负责任。
# 安装 $ yum install mariadb-server # 启动服务 $ systemctl start mariadb # 开机启动 $ systemctl enable mariadb
MariaDB默认root密码为空,我们需要设置一下,执行脚本:
$ sudo mysql_secure_installation
这个脚本会经过一些列的交互问答来进行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:
$ mysql -uroot -p
按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。
最后我们将MariaDB设置为开机启动。
$ sudo systemctl enable mariadb
mysql> grant all on *.* to 'root'@'%' identified by 'root'; mysql> flush privileges;
composer的大名,我就不想介绍了,如果你是一个phper,没用过,我也就不怪你,毕竟但是他现在才刚过1.0版,但是如果听都没听过,请面壁去……
安装说明
$ php -r “readfile(‘https://getcomposer.org/installer‘);” > composer-setup.php $ php composer-setup.php $ php -r “unlink(‘composer-setup.php’);”
上述 3 条命令的作用依次是:
下载安装脚本(composer-setup.php)到当前目录。
执行安装过程。
删除安装脚本 – composer-setup.php 。
全局安装是将 Composer 安装到系统环境变量 PATH 所包含的路径下面,然后就能够在命令行窗口中直接执行 composer 命令了。
Mac 或 Linux 系统:打开命令行窗口并执行如下命令将前面下载的 composer.phar 文件移动到 /usr/local/bin/ 目录下面:
$ sudo mv composer.phar /usr/local/bin/composer
然后执行:
composer -v
由于composer的包都在国外,这里设置一下composer的配置,让其每次运行时,都使用国内的包
$ composer config -g repo.packagist composer https://packagist.phpcomposer.com
查看composer的配置文件
vim /root/.composer/config.json
看到以上内容,表示配置成功!
推荐学习:php视频教程
The above is the detailed content of How to build a php7 environment with vagrant. For more information, please follow other related articles on the PHP Chinese website!