Loading... # 开始使用 vagrant ⅠⅠ (搭建 lnmp Nginx + PHP73 + Mysql57) ## 1. 使用 vagrant 创建 centos7 * 先创建一个 Vagrantfile 文件 ```bash [vagrant@lnmp-new ~]$ mkdir ~/vagrant/lnmp/ [vagrant@lnmp-new ~]$ vagrant init ``` * 添加以下内容 ``` config.vm.box = "generic/centos7" # 使用的 box ,会自动下载 config.vm.hostname = "lnmp-new" # ssh 进入镜像后的名字 config.vm.network :forwarded_port, guest: 80, host: 8060 #映射的端口访问 config.vm.synced_folder "/Users/biyongyao/www", "/var/www/html/" #映射的目录 config.vm.network "private_network", type: "dhcp" #动态获取 ip ``` * 启动 centos7 ```bash [vagrant@lnmp-new ~]$ vagrant up ``` * 启动 centos7 的 ssh ```bash [vagrant@lnmp-new ~]$ vagrant ssh ``` ## 2. 安装 Nginx > [参考文章](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7) * Add Nginx Repository (新添加一个商用包) ```shell shell> sudo yum install epel-release shell> rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm ``` * Install Nginx (安装 nginx) ```shell shell> sudo yum install nginx ``` * Start Nginx (启动、开机启动 nginx) ```shell shell> sudo systemctl start nginx shell> sudo systemctl enable nginx ``` * 防火墙允许开启 http 和 https 访问的端口 ```shell shell> sudo firewall-cmd --permanent --zone=public --add-service=http shell> sudo firewall-cmd --permanent --zone=public --add-service=https shell> sudo firewall-cmd --reload ``` * 安装的目录 1. 项目的目录 : `/usr/share/nginx/html` 2. nginx 配置文件: `/etc/nginx/nginx.conf` ## 3. 安装 Mysql57 > [参考文章 Mysql 官方文档](https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/) * 引入包 (可能需要 root 权限) ``` shell> sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm ``` * 查看包的 enable ``` shell> yum repolist all | grep mysql ``` - ![](https://gz-blog-storage-1252787757.cos.ap-guangzhou.myqcloud.com/usr/uploads/2024/05/17162853521016.jpg) * enable Mysql57 ``` shell> sudo yum-config-manager --disable mysql80-community shell> sudo yum-config-manager --enable mysql57-community ``` * 编辑文件 `/etc/yum.repos.d/mysql-community.repo` 设置 enabled = 1 ```config Enable to use MySQL 5.7 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql ``` * 查看 enabled 的 Mysql ``` shell> yum repolist enabled | grep mysql ``` - ![](https://gz-blog-storage-1252787757.cos.ap-guangzhou.myqcloud.com/usr/uploads/2024/05/17162853521035.jpg) * 安装 Mysql ,启动和开机启动 Mysql ``` shell> sudo yum install mysql-community-server shell> sudo systemctl start mysqld.service shell> sudo systemctl enable mysqld.service ``` * 查看安装日志找出密码 ``` shell> sudo grep 'temporary password' /var/log/mysqld.log ``` * 修改密码 ``` shell> mysql -uroot -p mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'; ``` * 注意问题 > 可能你的密码太简单导致你修改不了,如果是开发环境,设置简单的密码 > > set global validate_password_policy=0; > > set global validate_password_mixed_case_count=0; > > set global validate_password_number_count=3; > > set global validate_password_special_char_count=0; > > set global validate_password_length=3; > > SHOW VARIABLES LIKE 'validate_password%'; > ## 4. 编译安装 PHP73 * 参考文章 1. [PHP编译安装](http://www.zhongruitech.com/909060901.html) 2. [微信牛人文章](https://mp.weixin.qq.com/s?__biz=MzAxNzMwOTQ0NA%3D%3D&mid=2653354846&idx=2&sn=1ce663ac464de00530174fa949fd2e3a&chksm=8035d135b742582396d6f572107649058230b67d6b7e853a8a4c1e45e6579bc7aafd4536cfcd) 3. [特别感谢这篇文章纠正我低级错误](https://programmer.group/directory-index-of-usr-share-nginx-html-is-forbidden.html) * 安装编译的的依赖 ``` shell> sudo yum install gcc gcc-c++ libxml2-devel ``` > 上面这条命令是通过 yum 安装c和c++的语言环境,后面libxml2-devel是一个 C 语言操作xml的库,这样PHP也就可以操作 xml 了。 > 以上三个软件都是PHP必须的依赖。centos7.3版本当中 c 和 c++ 默认都是安装好的。 * 下载 php73 源码 [php.net](www.php.net) ``` shell> sudo yum install wget shell> wget https://www.php.net/distributions/php-7.3.6.tar.gz shell> tar -zvxf php-7.3.6.tar.gz shell> cd php-7.3.6 ``` * 配置安装目录 ``` shell> ./configure --prefix=/usr/local/php7 --enable-fpm ``` > 上面 configure 就是配置安装的信息,如 windows 中安装软件要选择目录下一步一样。 > 记得要使用 --enable-fpm 不然要重新安装哟 * 编译安装 ``` shell> sudo yum install make shell> make shell> sudo make install ``` * 配置 php 和测试是否安装成功 ```shell shell> /usr/local/php7/bin/php -v shell> ln -s /usr/local/php7/sbin/php-fpm /usr/bin/php-fpm shell> ln -s /usr/local/php7/bin/php /usr/bin/php shell> cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf shell> cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ``` * 修改 PHP-FPM 的配置 ```shell shell> sudo vi /usr/local/php7/etc/php-fpm.d/www.conf ``` ```config listen = 127.0.0.1:9001 ``` > 将 user 和 group 是 nobody 因为是虚拟机的开发环境,如果生产环境根据需求新建一个用户。 > 将监听端口改成 9001 ,因为端口 9000 和一些程序冲突比较多,也可以改用 socket 文件的共享。 * 开机启动 PHP-FPM `shell> sudo vim /lib/systemd/system/php-fpm.service` ``` [Unit] Description=php-fpm After=network.target [Service] Type=forking ExecStart=/usr/local/php7/sbin/php-fpm PrivateTmp=true [Install] WantedBy=multi-user.target ``` > 因为 php73 是通过编译安装的,所有有手动建立一个 php-fpm.service 进行开机启动 * 配置 Nginx 能转发 PHP 给 PHP-fpm `shell> sudo vi /etc/nginx/nginx.conf` ```config #将 index 设置为 php,不然或出现 403 forbidden,这个问题我花费了 2 个小时,原来这么简单的问题,所以看代码或者配置都要每行看,知其所以然 location / { root html; index index.php index.html index.htm; } ``` * 新建配置 `shell> sudo vi /etc/nginx/default.d/indexphp.default.conf` ```config location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9001; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; ``` } ``` * 测试 `shell> sudo vi /usr/share/nginx/html/index.php` ``` <?php echo phpinfo(); ``` * 通过浏览器打开 **localhost:8060** ![](https://gz-blog-storage-1252787757.cos.ap-guangzhou.myqcloud.com/usr/uploads/2024/05/17162853521056.jpg) ## 安装完成,进行打包 ``` vagrant package --output centos7_nginx1.14_mysql57_php73 ``` ``` © Reprint prohibited Support Appreciate the author AliPayWeChat Like If you think my article is useful to you, please feel free to appreciate