相信有些刚刚接触web开发的小伙伴对于服务器上搭建web环境还不太了解,今天手把手教大家搭建lemp的线上环境,您需要做如下一些准备:
- 阿里云或者其他服务商的云主机一台
- 云主机已安装Centos 7
- 了解ssh、vim
好的,相信大家已经做好准备了,那我们开始吧!
第一步,因为Centos 7源比较旧,所以我们导入官方拓展源
sudo yum install centos-release-scl
第二步,安装配置nginx
sudo yum install rh-nginx114
sudo systemctl start rh-nginx114-nginx
sudo systemctl status rh-nginx114-nginx
第三步(选做),如果您使用的是必须自己配置防火墙的主机商那么需要做这一步
sudo yum install firewalld
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload
第四步,测试能否访问到页面,如果可以设置默认启动,
sudo systemctl enable rh-nginx114-nginx
第五步,安装数据库,centos 7 mysql已被替换为mariadb,我们安装拓展库里的较新版本
sudo yum install rh-mariadb102
sudo systemctl start rh-mariadb102-mariadb
source /opt/rh/rh-mariadb102/enable
第六步,安全初始化
mysql_secure_installation
//是否设置密码 y 默认密码为空,删除测试库 y
第七步,设置默认启动
sudo systemctl enable rh-mariadb102-mariadb
第八步,安装php以及常用到的拓展()
sudo yum install rh-php72-php-fpm rh-php72-php-mysqlnd rh-php72-php-pecl rh-php72-php-pecl-apcu sclo-php72-php-pecl sclo-php72-php-pecl-mongodb sclo-php72-php-pecl-redis4 rh-php72-php-cli rh-php72-php-xml rh-php72-php-xmlrpc zip unzip rh-php72-php-zip
rh-php72-php-mbstring rh-php72-php-gd rh-php72-php-intl rh-php72-php-pear
第八步,消除cgi.fix_pathinfo的安全隐患
sudo vim /etc/opt/rh/rh-php72/php.ini
//找到行cgi.fix_pathinfo=1 然后将1改成0
第九步,更改运行时身份
sudo vim /etc/opt/rh/rh-php72/php-fpm.d/www.conf
//找到user = apache group = apache 改为;user = nginx
group = nginx
第十步,设计php-fpm默认启动
sudo systemctl start rh-php72-php-fpm
sudo systemctl enable rh-php72-php-fpm
第十一步,让php勾搭上nginx
sudo vim /etc/opt/rh/rh-nginx114/nginx/nginx.conf
//添加或修改总之出现如下代码快
location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
第十一步,验证php是否运行正常
sudo systemctl reload rh-nginx114-nginxvim /opt/rh/rh-nginx114/root/usr/share/nginx/html/demo.php
<?php phpinfo(); ?>//然后访问页面看看是否成功
第十一步,别忘删掉测试页面
sudo rm /opt/rh/rh-nginx114/root/usr/share/nginx/html/demo.php