您的位置:首页 > 运维架构 > Nginx

centos7搭建lnmp (php7 + mysql5.7 +nginx1.8)最新版

2018-06-01 09:33 441 查看
以下是本人自己整理的安装步骤,方便大家快速配置自己的lnmp环境
1.php7编译安装

  

  1.1首先下载并解压php安装包 php下载地址

           下载

[html]  查看纯文本 
  1. wget http://cn2.php.net/get/php-7.2.4.tar.xz/from/this/mirror  

           解压(tar解压命令-x解压-v显示所有过程-f使用档案名字)    

[html]  查看纯文本 
  1. tar -xvf镜像   

     1.2安装

         这个时候你已经得到了解压后的PHP的安装包         进入解压后的文件夹

[html]  查看纯文本 
  1. cd php-7.2.4 /  

        安装依赖

[html]  查看纯文本 
  1. yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel  
[html]  查看纯文本 
  1.   

        配置php(现在只是安装一下必要的扩展,等你需要额外的扩展完全可以以后再安装)

[html]  查看纯文本 
  1. ./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   

        注意这个地方会很容易出现下面的错误
 执行yum install -y gcc 就可以了 再次执行以上代码就ok

--prefix = / usr / local / php这个是你的安装目录    
  1. --with-mysqli这个是mysqli扩展   
  2. --with-fpm-user = nginx  fpm用户  
  3. --with-fpm-group-nginx fpm用户组  

        当你看到如下页面就代表PHP可以安装了

         执行命令[html]  查看纯文本 
  1. make && make install  

         添加PHP命令到环境变量    复制配置文件

[html]  查看纯文本 
  1. cp /root/php-7.2.4/php.ini-development /usr/local/php/lib/php.ini  
[html] view plain copy
  1. vim /etc/profile  
  2. 在结尾添加  
  3. PATH=$PATH:/usr/local/php/bin  
  4. export PATH  
  5. 保存后运行  
  6. source /etc/profile  
  7. 执行命令  
  8. php -v   
  9. 如果出现以下画面就代表php安装成功  

       配置php-fpm  1.将启动文件加入systemctl,复制到系统文件夹中

[html] view plain copy
  1. cp /root/php-7.2.4/sapi/fpm/php-fpm.service /usr/lib/systemd/system  

  2.配置

[html] view plain copy
  1. 1.生成 php-fpm.conf  
  2.   cd /usr/local/php/etc/  
  3. 复制默认文件  
  4.   cp php-fpm.conf.default php-fpm.conf  
  5. 2.生成 www.conf  
  6.   cd /usr/local/php/etc/php-fpm.d  
  7. 复制默认文件  
  8.   cp www.conf.default www.conf  
  9. 编辑文件  
  10.   vim www.conf  
  11. 将其中  
  12.   user = nginx  
  13.   group = nginx  
  14. 改为  
  15.   user = nobody  
  16.   group = nobody  

    3.启动

[html] view plain copy
  1. systemctl start php-fpm.service   
  2. 查看状态,如显示如下就代表启动成功  

2.nginx1.8安装

   安装

[html] view plain copy
  1. rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm  
[html] view plain copy
  1. yum install nginx  

  启动

[html] view plain copy
  1. systemctl start nginx.service   
  2. 查看状态 如果出现以下画面就带代表安装成功  
  3. systemctl status nginx.service  
   配置站点[html] view plain copy
  1. 创建文件夹   
  2.   mkdir /www  
  3. 创建phpinfo文件  
  4.   vim /www/phpinfo.php  
  5. 编辑添加  
  6.   <?php  
  7.     echo phpinfo();  
  8. 编辑nginx配置  
  9.   vim /etc/nginx/nginx.conf  


   1.修改网站根目录        把root改为/www
   2.添加php解析

[html] view plain copy
  1. location ~ \.php$ {  
  2.     fastcgi_pass   127.0.0.1:9000;  
  3.     fastcgi_index  index.php;  
  4.     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
  5.     include        fastcgi_params;  
  6. }  

   重启

[html] view plain copy
  1. systemctl restart nginx.service  

   访问,出现以下页面就代表nginx已经可以解析php文件了

[html] view plain copy
  1. 你的域名/phpinfo.php  


3.mysql5.7安装      安装

[html] view plain copy
  1. rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm  
[html] view plain copy
  1. yum install mysql-community-server mysql-community-devel  

      启动

[html] view plain copy
  1. 启动  
  2. systemctl start mysqld  
  3. 查看状态,显示下图就代表启动成功  
  4. systemctl status mysqld  

     获取初始密码,最后就是初始密码了

[html] view plain copy
  1. grep 'temporary password' /var/log/mysqld.log  

    进入mysql

[html] view plain copy
  1. mysql -u root -p  

    修改密码

[html] view plain copy
  1. ALTER USER 'root'@'localhost' IDENTIFIED BY '你的密码'; 

  2.  

远程连接数据库:1. 在服务器内部登录数据库,然后执行grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;     此语句意思是给root用户在任何IP都可以登录数据库然后执行 flush privileges;//刷新数据库权限
其中: *.* 第一个*是指数据库*代表所有数据库第二个*指数据库对象*代表数据库中所有对象 'root'@'%' root是制定要授权的数据库用户%代表允许登录的IP123456是你的数据库密码假如要限制只能在1.1.1.1登录,把%改成1.1.1.1 ,执行完毕后执行 flush privileges; 刷新权限   然后在本地电脑mysql客户端就可以连接了。
  配置,修改php.ini 这个值可以从 /etc/my.cnf 找到

[html] view plain copy
  1. vim php.ini  
  2. 修改  
  3. mysqli.default_socket = /var/lib/mysql/mysql.sock  

重启nginx和php-fpm

[html] view plain copy
  1. systemctl restart nginx.service  
  2. systemctl restart php-fpm.service  
  添加文件到/www文件夹中[html] view plain copy
  1. vim /www/testMysql.php  
添加内容为[html] view plain copy
  1. <?php  
  2.         $ mysqli  = @new mysqli('localhost','root','你的密码');  
  3.         if($ mysqli- > connect_errno){  
  4.                 die(“无法连接到数据库:\ n”。$ mysqli- > connect_error);  
  5.         }其他{  
  6.                 回声'连接成功!';  
  7.         }           
现在你访问域名/testMysql.php如果显示连接成功就代表lnmp环境搭建成功了





阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐