新年伊始,有幸给刚入职的PHP开发小伙伴们主持成军训练,为此写了本文,方便大家一步步用Vmware虚拟机搭建起CentOS+Nginx+MariaDB+PHP7+Git的开发环境。因为主要在开发者个人计算机中运行,因此在安全和性能调优方面完全没有涉及。
CentOS7
- 下载CentOS 7.5 64位操作系统光盘安装镜像文件,在Vmware Workstation Player中建立新虚拟机,使用前述镜像文件安装操作系统,将虚拟磁盘拆分成多个文件,内存1G,处理器1个,网络适配器选“桥接模式”;

- 进入CentOS安装向导后,选择最小配置软件、设置国内时区、时间、语言支持增加中文、设置主机名、配置局域网,设置root用户密码;
- 完成系统安装后使用root用户登录,更新系统;
- 安装网络工具。
Nginx 1.12.2
- 安装依赖项;
1
| yum install -y wget gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel curl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker
|
- 下载后编译安装;
1
| wget https://nginx.org/download/nginx-1.12.2.tar.gztar zxvf nginx-1.12.2.tar.gzcd nginx-1.12.2./configure --with-http_ssl_modulemake && make install
|
- 启动Nginx,添加Nginx可执行文件路径到系统路径;
1
| cd /usr/local/nginx/sbin./nginxexport PATH=$PATH:/usr/local/nginx/sbin
|
- 配置防火墙;
1
| firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=3306/tcp --permanentfirewall-cmd --zone=public --add-port=9000/tcp --permanentfirewall-cmd --reload
|
- 将Nginx设置为自启动项;
将以下内容复制到此文件中:
1
| #!/bin/sh# nginx - this script starts and stops the nginx daemin. /etc/rc.d/init.d/functions. /etc/sysconfig/network[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { configtest || return $? stop start}reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo}force_reload() { restart}configtest() { $nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit 2esac
|
然后保存退出,启动服务,以后就可以用service nginx start|stop|restart方式进行管理了。
1
| cd /etc/init.d/chmod +x ./nginxchkconfig --add /etc/init.d/nginxchkconfig nginx on
|
MariaDB
- 安装软件;
- 启动服务;
1
| systemctl start mariadb.servicesystemctl enable mariadb.service
|
- 设置root用户密码。
1
| mysqladmin -u root password "password here"
|
PHP7
- 安装软件源;
1
| yum install epel-releaserpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
|
- 安装PHP7及扩展;
1
| yum -y install php70wyum -y install php70w-xml php70w-xmlrpc php70w-mbstring php70w-json php70w-gd php70w-mcrypt php70w-fpm php70w-mysql php70w-pdo php70w-opcache php70w-pecl-xdebug php70w-pecl-imagick php70w-common php70w-cli php70w-devel
|
- 按开发要求修改PHP设置;
修改设置如下:
1
| display_errors=Onfile_uploads onupload_max_filesize 30mpost_max_size 30m
|
配置后重启。
- 启动FPM;
1
| systemctl start php-fpmsystemctl enable php-fpm
|
- 配置Nginx;
1
| vi /usr/local/nginx/conf/nginx.conf
|
文件头部增加user root;
http{}块内增加:client_max_body_size 30m;
在server{}块内增加:
1
| location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index default.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; } include /usr/local/nginx/conf/vhosts/*.conf;
|
保存退出,重启Nginx。
phpMyAdmin
- 安装软件;
- 脚本所在目录链接到网站目录;
1
| ln -s /usr/share/phpMyAdmin/ /usr/local/nginx/html/
|
- 重启PHP。
1
| systemctl restart php-fpm
|
VSFTPD
- 安装软件;
- 创建用户,设密码和主目录后启动服务;
1
| useradd ftpuser -s /sbin/nologinpasswd ftpuserusermod -d /usr/local/nginx/html/ ftpuserservice vsftpd restart
|
3. 修改防火墙;
1
| firewall-cmd --zone=public --add-port=21/tcp --permanentfirewall-cmd --add-service=ftp --permanent
|
- 修改配置;
1
| vi /etc/vsftpd/vsftpd.conf
|
修改设置项为:
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=000
chroot_local_user=YES
user_config_dir=/etc/vsftpd/userconfig
allow_writeable_chroot=YES
注释掉以下设置项:
#anon_upload_enable=YES
#anon_other_write_enable=YES
#anon_mkdir_write_enable=YES
5. 添加ftpuser用户的配置文件;
1
| cd /etc/vsftpdmkdir userconfigcd userconfigvi ftpuser
|
添加内容:
1
| local_root=/usr/local/nginx/html/
|
后保存退出。
6. 修改登录认证方式;
注释掉auth required pam_shells.so
7. 设置自动启动,然后重启服务;
1
| chkconfig vsftpd onservice vsftpd restart
|
- 修改ftp目录权限。
1
| cd /usr/local/nginxchown -R ftpuser htmlchmod 755 html
|
Git 1.8.3.1
- 下载和解压缩;
1
| cd ~wget https://www.kernel.org/pub/software/scm/git/git-1.8.3.1.tar.gztar xzf git-18.8.3.1.tar.gz
|
- 编译和安装;
1
| cd git-1.8.3.1make prefix=/usr/local/git allmake prefix=/usr/local/git installecho "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrcsource /etc/bashrc
|
- 设置用户名,邮件,提交信息等;
将.gitmessage提交信息模板文件传入虚拟机中,放入用户目录下
1
| git config --global user.name "Your Name"git config --global user.email email@gmail.comgit config --global user.console "Console Name"git config --global commit.template "~/.gitmessage"
|
- 生成密钥;
1
| cd ~mkdir .sshcd .sshssh-keygen -t rsa
|
连续回车两次生成密钥,然后把id_rsa.pub文件复制出来。如果你已经在别的机器上生成过密钥,也可以把id_rsa和id_rsa.pub文件复制到/.ssh目录下,省去以上生成的步骤。
5. 公钥添加到Git主服务器;
1. 登录git服务器,切换到git用户;
2. 进入/.ssh/目录,新建new.pub文件,将刚才复制出来的id_rsa.pub公钥文件的内容贴入保存;
3. 将new.pub文件内容加入已有公钥文件表中。
{% codeblock lang:bash %}
cat new.pub >> authrized_keys
{% endcodeblock %}
- 回到虚拟机中就可以免密码登录Git主服务器,clone代码项目回开发环境了。
IDE
使用Atom编辑器做IDE的小伙伴,可以安装Remote FTP插件,将虚拟机的登录信息设置进去。只要虚拟机开机,在Atom中连接FTP服务器,就可以在保存代码时自动更新到开发环境中,方便测试。

