阿里云Centos7 yum安装PHP7.2 LNMP环境配置 (centos7.6+nginx+MySQL5.7+PHP7.2)

首先更新系统软件

$ yum update

安装nginx
1.安装nginx源:

yum localinstall

2.安装nginx

yum install nginx

3.启动nginx

service nginx start

会出现:Redirecting to /bin/systemctl start  nginx.service

4.访问http://你的ip/

如果成功安装会出来nginx默认的欢迎界面


安装MySQL5.7.*

安装mysql源

yum localinstall

安装mysql

yum install mysql-community-server

确认一下mysql的版本

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

yum install mysql-community-devel

启动mysql

service mysqld start

查看mysql启动状态

service mysqld status

出现pid,证明启动成功


获取mysql默认生成的密码

grep 'temporary password' /var/log/mysqld.log
2015-12-05T05:41:09.104758Z 1 [Note] A temporary password is generated for root@localhost: %G1Rgns!dD!v

localhost后的就是密码

换成自己的密码

mysql -uroot -p

Enter password:输入上面的密码

更换密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxx';

这个密码一定要尽量复杂,不然可能提示密码不合法;

退出mysql;

mysql> quit;

用新密码再登录,试一下新密码

$ mysql -uroot -p

Enter password:输入你的新密码

确认密码正确后,退出mysql;

mysql> quit;



编译安装php7.2.16

下载php7源码包

cd /root & wget -O php7.2.16.tar.gz

解压源码包

$ tar -xvf php7.2.16.tar.gz

$ cd php-7.2.16

安装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

编译配置,这一步如果遇到很多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-11解决,否则直接跳到 --- 编译与安装;

configure error:

1.configure: error: xml2-config not found. Please check your libxml2 installation.
解决:
1.    $ yum install libxml2 libxml2-devel

2.configure: error: Cannot find OpenSSL's <evp.h>
解决:$ yum install openssl openssl-devel

3.configure: error: Please reinstall the BZip2 distribution
解决:$ yum install bzip2 bzip2-devel

4.configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
解决:$ yum install libcurl libcurl-devel

5.If configure fails try --with-webp-dir=<DIR> configure: error: jpeglib.h not found.
解决:$ yum install libjpeg libjpeg-devel

6.If configure fails try --with-webp-dir=<DIR>

checking for jpeg_read_header in -ljpeg... yes

configure: error: png.h not found.

解决:$ yum install libpng libpng-devel

7.If configure fails try --with-webp-dir=<DIR>

checking for jpeg_read_header in -ljpeg... yes

checking for png_write_image in -lpng... yes

If configure fails try --with-xpm-dir=<DIR>

configure: error: freetype-config not found.

解决:$ yum install freetype freetype-devel

8.configure: error: Unable to locate gmp.h
解决:$ yum install gmp gmp-devel

9.configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:$ yum install libmcrypt libmcrypt-devel

10..configure: error: Please reinstall readline - I cannot find readline.h
解决:$ yum install readline readline-devel

11.configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解决:$ yum install libxslt libxslt-devel


编译与安装

make && make install

这里要make一会,要耐心一下


特别说明:一般因为这里会因为swap没有设置大小而现实编译失败,以下为步骤:

free -m   (查看swap情况,会发现swap 是0,不代表全部,我的阿里云7.6的是)

以下开始操作:  使用下面的命令创建2G的空间

dd if=/dev/zero of=/var/swap bs=1024 count=2048000

if 表示infile,of表示outfile,bs=1024代表增加的模块大小,count=2048000代表2048000个模块,也就是2G空间
将目的文件设置为swap分区文件

mkswap /var/swap

激活swap,立即启用交换分区文件

 mkswap -f /var/swap
 
 swapon /var/swap

查看swap情况

free -m

cat /proc/swaps

vim /etc/fstab

最后一行添加

/var/swap swap swap defaults 0 0

重启或free -m测试 swap添加成功

如果不再需要swap,可以清理该分区:

swapoff /var/swap

然后重新

 make && make install

等编译完成后会有提示:

Barley博客


添加 PHP 命令到环境变量

vim /etc/profile

在末尾加入

PATH=$PATH:/usr/local/php/bin

export PATH

要使改动立即生效执行

./etc/profile或是$ source /etc/profile

查看环境变量

echo $PATH

查看php版本

php -v

Barley博客

配置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

启动php-fpm

/etc/init.d/php-fpm start

配置nginx虚拟机,绑定域名

vim /etc/nginx/conf.d/xxx.barleyz.com.conf


server{
    listen 80;
    server_name  xxx.barleyz.com;
    root /var/www/xxxx/public; # 该项要修改为你准备存放才程序的路径
    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;
    }
}


重启nginx

service nginx reload

创建测试php文件

vim /var/www/xxxx/public/index.php

把下面的代码复制到这个文件 里

<?php

phpinfo();

保存退出,然后按照nginx里的配置域名访问即可

Barley博客
  • 最新评论
  • 总共0条评论