Centos7 搭建lnmp环境 (centos7+nginx+MySQL5.7.9+PHP7)

一、安装nginx
首先更新系统软件

# yum update

安装nginx这里写代码片
1.安装nginx源

# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安装nginx

# yum install nginx

3.启动nginx

# service nginx start

显示:Redirecting to /bin/systemctl start nginx.service
4.访问http://你的ip/
如果成功安装会出来nginx默认的欢迎界面
Nginx成功页
二、安装MySQL5.7.*
1.安装mysql源

# yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安装mysql

# yum install mysql-community-server

3.安装mysql的开发包,以后会有用

# yum install mysql-community-devel

4.启动mysql

# service mysqld start

显示:Redirecting to /bin/systemctl start mysqld.service
5.查看mysql启动状态

# service mysqld status

出现pid
证明启动成功
6.获取mysql默认生成的密码

# grep 'temporary password' /var/log/mysqld.log

mysql默认密码
选中的就是默认密码。
7.换成自己的密码

# mysql -uroot -p

Enter password:输入上页的密码,进入mysql
8.更换密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPasdfs4!';

这个密码一定要足够复杂,不然会不让你改,提示密码不合法;
9.退出mysql并试用下新密码

mysql> quit;
mysql -uroot -p

确认密码正确
三、编译安装php7.0.0
1.下载php7源码包

# cd /root & wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

没有安装wget的,先安装:yum install wget
2.解压源码包

# tar -xvf php7.tar.gz

3.进入目录

# cd php-7.0.1

4.安装php依赖包 

# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

5.编译配置,这里如果上一步的某些依赖包没有安装好,就会遇到很多configure error,我们一一解决,安装上相关软件开发包就可以

# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

这里可能会出现一些报错,下面列举一些报错和对应解决方式(解决方式可能已过时):
1)configure: error: no acceptable C compiler found in $PATH See ‘config.log’ for more details
解决:#yum install gcc
2)configure: error: xml2-config not found. Please check your libxml2 installation.
解决:# yum install libxml2 libxml2-devel
3)onfigure: error: Cannot find OpenSSL’s
解决:# yum install openssl openssl-devel
4)configure: error: Please reinstall the BZip2 distribution
解决:# yum install bzip2 bzip2-devel
5)configure: error: Please reinstall the libcurl distribution - easy.h should be in /include/curl/
解决:# yum install libcurl libcurl-devel
6)configure: error: jpeglib.h not found.
解决:yum install libjpeg libjpeg-devel
7)configure: error: png.h not found.
解决:yum install libpng libpng-devel
8)configure: error: freetype-config not found.
解决:# yum install freetype freetype-devel
9)configure: error: Unable to locate gmp.h
解决:# yum install gmp gmp-devel
10)configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:# yum install libmcrypt libmcrypt-devel
11).configure: error: Please reinstall readline - I cannot find readline.h
解决:# yum install readline readline-devel
12)configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决:# yum install libxslt libxslt-devel
这里就列举12条报错,具体遇到了再找对应的解决方式,没有再报错了就下一步了
6.编译与安装

# make && make install

这里要make好久,要耐心一下
7.添加 PHP 命令到环境变量

# vim /etc/profile

在末尾加入
PATH=$PATH:/usr/local/php/bin
export PATH
要使改动立即生效执行

# source /etc/profile

查看环境变量

# echo $PATH

查看php版本

# php -v

8.配置php-fpm

# cp php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

9.启动php-fpm

# /etc/init.d/php-fpm start

四、配置nginx虚拟机,绑定域名
1.绑定域名

# vim /etc/nginx/conf.d/php7.aaa.com.conf

可以把php7.aaa.com.conf改成自己的域名
把下面的内容复制到php7.aaa.com.conf里

server{
    listen 80;
    server_name  php7.aaa.com;
    root /var/www/html/php7.aaa.com; # 该项要修改为你准备存放相关网页的路径
    location / {
        index  index.php index.html index.htm;
         #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
         if (!-e $request_filename)
         {
            #地址作为将参数rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目录则使用下面这句,将subdir改成目录名称即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
         }
    }
    #proxy the php scripts to php-fpm
    location ~ \.php {
            include fastcgi_params;
            ##pathinfo支持start
            #定义变量 $path_info ,用于存放pathinfo信息
            set $path_info "";
            #定义变量 $real_script_name,用于存放真实地址
            set $real_script_name $fastcgi_script_name;
            #如果地址与引号内的正则表达式匹配
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    #将文件地址赋值给变量 $real_script_name
                    set $real_script_name $1;
                    #将文件地址后的参数赋值给变量 $path_info
                    set $path_info $2;
            }
            #配置fastcgi的一些参数
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            ###pathinfo支持end
        fastcgi_intercept_errors on;
        fastcgi_pass   127.0.0.1:9000;
    }

    location ^~ /data/runtime {
    return 404;
    }

    location ^~ /application {
    return 404;
    }

    location ^~ /simplewind {
    return 404;
    }
}

2.重启nginx

# service nginx reload

3.在php7.aaa.com.conf里面root对应的路径(/var/www/html/php7.aaa.com)下建index.php文件

# vim /var/www/html/php7.aaa.com/index.php

没有安装vim的,可以使用系统自带的vi,或者先安装先安装:yum install vim
把下面的代码复制到这个文件 里

<?php
phpinfo();

查看访问http://php7.aaa.com

之前搞过几次编译搭建lnmp环境都以失败告终,搞一键安装lnmp环境是成功了,但是没啥成就感,心里一直惦记着编译搭建。今天看到 炉火纯青 的博客文章,按照步骤同时百度搭建过程中的问题,终于搭建成功了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值