VMware14中安装CentOS 7 并且搭建 php7.2 + nginx 1.8 + mysql 5.7 环境(服务器也适用)以及本地文件与虚拟机linux环境共享的方法(方便开发)

VMware workstation 14中安装CentOS 7:

详见:https://blog.csdn.net/qq_36433857/article/details/81909348

系统安装好后配置系统

一、防火墙配置

CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

systemctl mask firewalld

systemctl stop firewalld

yum remove firewalld

2、安装iptables防火墙

yum install iptables-services #安装
vi /etc/sysconfig/iptables #编辑防火墙配置文件

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

/usr/libexec/iptables/iptables.init restart #重启防火墙

二、关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

三、centOS7准备软件

1、gcc

# yum install gcc
接着输入y就可以了

2、g++

# yum install gcc-c++
接着输入y就可以了

3、gdb

# yum install gdb

4、autoconf

# yum -y install autoconf

5、automake

# yum -y install automake

6、libtool

# yum -y install libtool

7、pcre

# yum install pcre-devel

8、更新yum源

# yum update

9、安装编译工具及库文件(使用yum命令安装)

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

以上准备工作就绪整个系统就准备好了,接下来开始安装我们需要的php7.1 + nginx1.8 + mysql5.7
在根目录进行操作即可

小技巧:

sudo find / -name php.ini  #查找自己想要找到的文件
/cgi.fix # 这个是vim修改内容时查找 按n 可以查找下一个 类似windows 下的 ctrl+f

四:安装php

#下载安装包
wget http://cn2.php.net/get/php-7.2.4.tar.xz/from/this/mirror

#解压

tar -xvf mirror 

#进入到解压的文件
cd php-7.2.4/

#安装依赖
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel

# 配置php必要的一些扩展
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip

#执行命令编译安装(要等6到7分钟)

make && make install

#添加 PHP 命令到环境变量

cp /root/php-7.2.4/php.ini-development /usr/local/php/lib/php.ini

#配置

vim /etc/profile
#在结尾添加
PATH=$PATH:/usr/local/php/bin
export PATH
#保存后运行
source /etc/profile
#执行命令
php -v 
如果出现版本号和版权就代表成功了

五、配置php-fpm

#将启动文件加入systemctl,复制到系统文件夹中
cp /root/php-7.2.4/sapi/fpm/php-fpm.service /usr/lib/systemd/system

#配置

#生成 php-fpm.conf

cd /usr/local/php/etc/

#复制默认文件

cp php-fpm.conf.default php-fpm.conf

#生成 www.conf

cd /usr/local/php/etc/php-fpm.d

#复制默认文件

cp www.conf.default www.conf

#编辑文件

vim www.conf

#将其中
user = nginx
group = nginx

#改为
user = nobody
group = nobody

#启动
systemctl start php-fpm.service 

#查看状态
systemctl status php-fpm.service
#有active(runing)字样代表成功

六、安装nginx

#安装
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

#启动
systemctl start nginx.service
#查看状态
systemctl status nginx.service
#有active(runing)字样代表成功

#配置站点
#创建文件夹 
mkdir /www

#创建phpinfo文件
vim /www/phpinfo.php

#编辑添加
<?php
echo phpinfo();

#编辑nginx配置
vim /etc/nginx/conf.d/default.conf

#按照图片做就对了

VMware14中安装CentOS 7 并且搭建 php7.2 + nginx 1.8 + mysql 5.7 环境(服务器也适用)以及本地文件与虚拟机linux环境共享的方法(方便开发)
#修改说明
#修改网站根目录

把root改为/www

#添加php解析

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;
}

#重启
systemctl restart nginx.service

访问方式:服务器ip/phpinfo.php 出现phpinfo信息就成功了

七、安装mysql

#安装
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum install mysql-community-server mysql-community-devel

#启动
systemctl start mysqld

#查看状态
systemctl status mysqld
#有active(runing)字样代表成功

#mysql 5.7有初始密码,获取密码方式为,最后的字符串就是密码
grep 'temporary password' /var/log/mysqld.log

#进入mysql 准备修改默认密码
mysql -u root -p

---------------------------------------------------------------------------

遇到的问题:Access denied for user 'root'@'localhost' (using password: YES) 
进去后输入密码说没有访问权限(Access denied),有可能是我中间退出过?密码失效了?我记得好像没有,这个问题怎么解决呢?

#问题解决办法,编辑my.cnf跳过权限验证

vim /etc/my.cnf
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程
:wq! #保存

#重启mysql
systemctl restart mysqld.service

#再次进入 p后随便输入面 中间不要有空格 回车
mysql -u root -p1111111
然后随便输入什么 回车,就可以进入数据库了

#然后修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码';
#生效
flush privileges;
#退出
quit;

#再次编辑 删除刚才加的skip-grant-tables 保存退出
vim /etc/my.cnf

#重新进入mysql 用新密码就可以了

--------------------------------------------------------------------------

#配置,修改php.ini 两处

vim /etc/php.ini
#修改
mysqli.default_socket = /var/lib/mysql/mysql.sock

vim /usr/local/php/lib/php.ini
#修改
mysqli.default_socket = /var/lib/mysql/mysql.sock

#重启nginx和php-fpm

systemctl restart nginx.service
systemctl restart php-fpm.service

#添加文件到/www文件夹中
vim /www/testMysql.php

#内容为
<?php
        $mysqli = @new mysqli('localhost','root','你的密码');
        if($mysqli->connect_errno){
                die("could not connect to the database:\n" . $mysqli->connect_error);
        }else{
                echo 'connect success!';
        }        

访问方式: 服务器ip/testMysql.php 如果显示 connect success就代表成功了

-----------------------------------END Success---------------------------------------

#其他命令 更具自己需要

systemctl enable php-fpm #设置开机启动
systemctl enable mysqld #设置开机启动
systemctl enable nginx #设置开机启动
php -v #查看php版本
nginx -v #查看nginx版本
netstat -an | grep 3306 #查看端口是否启用
netstat -apn | grep 9503 #查看服务端口
kill -9 18669 #停止服务 18669根据具体数字更改
php --ri swoole #查看swoole当前版本
pecl upgrade swoole #更新swoole版本
php composer.phar  #composer使用
composer #查看是否安装
su - admin #切换用户
yum install libhiredis-devel #安装hiredis
touch 1.txt #创建文件
rm -f 1.txt #删除文件
rm -r 1.txt #删除文件
rm -rf XXX #删除文件夹

#我在linux下安装软件的时候遇到的问题
1、
------------------------------------------------------------------------------
遇到的问题:[Errno 5] [Errno 12] 无法分配内存?
解决办法增加虚拟内存:
1、sudo dd if=/dev/zero of=/home/swap bs=64M count=16 //注释:of=/home/swap,放置swap的空间; count的大小就是增加的swap空间的大小,64M就是块大小,这里是64MB,所以总共空间就是bs*count=1024MB.这里分配空间的时候需要一点时间,等待执行完毕。
2、sudo mkswap /home/swap (可能会提示warning: don't erase bootbits sectorson whole disk. Use -f to force,不用理会)//把刚才空间格式化成swap各式
3、sudo swapon /home/swap 注释:使刚才创建的swap空间
-----------------------------------------------------------------------------
2、
------------------------------------------------------------------------------
遇到的问题:用navcate远程连接虚拟机数据库的时候不行,提示XXXX.XXX.xxx.x is not allowed to connect to this mysql server
解决办法:是你的帐号不允许从远程登陆,只能在
localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改"mysql" 数据库里的 "user" 表里的 "host"项,从"localhost"改称"%"


在安装mysql的机器上运行:
mysql -u root -pvmware
mysql>usemysql;
mysql>update user set host = '%' where user ='root';
mysql>select host, user from user; 
-----------------------------------------------------------------------------

3、
--------------------------------------------------------------------------

遇到的问题:虚拟机系统里的环境怎么运行本地(windows)里面的php程序呢?
解决办法本地共享挂载文件夹:

# cd /mnt/win/www  第一次是没有文件的,先执行后面的语句
#共享文件操作 虚拟机
vmware-hgfsclient  #可以看到本地共享的文件夹名称
mkdir /mnt/win #然后创建文件夹
/usr/bin/vmhgfs-fuse .host:/ /mnt/win -o subtype=vmhgfs-fuse,allow_other  #挂载文件 虚拟机关闭一次就要执行一次这个语句,因为虚拟机关闭后自动解除挂载
#编辑nginx配置
vim /etc/nginx/conf.d/default.conf
把root 后面的路径改为 上面设置的路径/mnt/win/www
#重启
systemctl restart nginx.service
#ip访问即可

--------------------------------------------------------------------------