首页 » 技术分享 » lnmp环境搭建(php7.2.4)

lnmp环境搭建(php7.2.4)

 
文章目录

一、虚拟机

强推vmware,比virtual box好上很多,virtual box 跑起来感觉好卡,而且动不动就内存xxoo报错直接闪退了,用vmware还没出现过这些问题,也不会很卡,跟windows的切换也比较方便,这个是我一般用来安装的镜像和虚拟机,可以自行下载一波

1.centos7.3镜像下载

2.vmware虚拟机下载


二、php部分

1.下载php镜像

下载
wget http://cn2.php.net/get/php-7.0.28.tar.gz/from/this/mirror
下载完成之后是镜像文件,需要将其重命名为tar.gz文件
mv mirror php-7.0.28.tar.gz
解压
tar -zxvf php-7.0.28.tar.gz

2.参数配置

配置前要安装/更新libxml2以及libxml2-devel

yum -y install libxml2 libxml2-devel

3.组件安装

把没有安装好的组件安装好(如果安装了就会进行升级)

yum -y install openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel

4.编译配置

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir 
--with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 
--with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql 
--with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl 
--with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml 
--enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring 
--enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets 
--enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

(注意,7.2不支持enable-gd-native-ttf这一项,要去掉)

5.执行编译

make (编译时间比较长)
编译完成之后执行安装 make install
如果出现In function 'do_convert' .....undefined reference to 'libiconv_open'....的问题,需要到MakeFile文件中找到EXTRA_LIBS,然后在后面加上 -liconv

6.配置相应文件

(主要是将配置文件复制到php根文件夹或者将根文件夹下的文件重命名)

cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

设置php.ini文件设置cgi.fix_pathinfo=0
这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0

7.创建web用户

groupadd www-data
useraddd www-data www-data

(下面这步可以跳过,因为在前面编译的时候已经指定了prefix。)
打开/usr/local/php/etc/php-fpm.comf,最后一行,include=NONE/etc/php-fpm.d/*.conf 前面NONE改为/usr/local)

然后修改文件名

cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vi /usr/local/php/etc/php-fpm.d/www.conf

修改user 和group 设置为前面创建的www-data

启动php-fpm 直接输入php-fpm
如果是重启php-fpm 则输入/etc/init.d/php-fpm restart


三、nginx部分

1.下载nginx

wget http://nginx.org/download/nginx-1.4.7.tar.gz
解压,然后./configure 执行配置
编译然后执行安装make && make install
跑nginx可能会出现 nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
则需要执行/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf指定nginx.conf的位置

2.修改nginx.conf文件

前面的user的值注意看看是不是www-data
去掉location php那一块的注释

location /{
    root    /home/www  #网站根目录
    index    index.html index.htm    index.php
}

location ~ [^/]\.php(/|$){
    root    /home/www;   #网站根目录
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index    index.php;
    fastcgi_param    SCRIPT_FILENAME    SCRIPTE_NAME/$fast_script_name;
    include    fastcgi_params;
    include    fastcgi.conf;

}

重启nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

3.设置php-fpm和nginx自启动

NGINX SHELL脚本 放到/etc/init.d/下取名nginx

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # chkconfig: 2345 90 91 # description: nginx web server# processname: nginx# config: /opt/nginx/conf/nginx.conf# pidfile: /opt/nginx/nginx.pid

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network


if [ -f /etc/sysconfig/nginx ];then
. /etc/sysconfig/nginx
fi

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx #-c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t #-c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
test)
configtest
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"
exit 2
esac

PHP-FPM SHELL脚本 放到/etc/init.d/下 取名php-fpm

#!/bin/sh  
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  

stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  

restart() {  
    configtest || return $?  
    stop  
    start  
}  

reload() {  
    configtest || return $?  
    echo -n $"Reloading $prog: "  
    killproc ${phpfpm} -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

rh_status() {  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2  

esac    

后续处理:

添加执行权限
chmod a+x /etc/init.d/nginx
chmod a+x /etc/init.d/php-fpm

加入服务
chkconfig --add nginx
chkconfig --add php-fpm

开机自启
chkconfig nginx on
chkconfig php-fpm on

顺便可以设一下快捷指令

`vi ~/.bashrc`
alias nginx='/etc/init.d/nginx'
alias php-fpm='/etc/init.d/php-fpm'
source ~/.bashrc

这样就可以直接通过nginx指令和php-fpm指令进行操作而不用通过绝对路径的指令进行操作


四、mysql安装

1.下载

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-57-community-release-el7-10.noarch.rmp
yum -y install mysql-community-server(这一步会花很长时间)
systemctl start mysqld.service
可以查看一下mysql运行状态
systemctl status mysqld.service

2.修改初始密码

获取mysql初始密码 grep "password" /var/log/mysqld.log
进入mysql输入上面获取的密码
然后修改密码规则:
set global validate_password_policy=0;
set global validate_password_length=1;
修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD'

3.可视化数据库phpmyadmin

wget https://files.phpmyadmin.net/phpMyAdmin/4.8.0.1/phpMyAdmin-4.8.0.1-all-languages.tar.gz
下载之后解压到网站根目录
把phpmyadmin下面的config.sample.inc.php改名为config.inc.php
将里面的host参数由localhost改成127.0.0.1 否则可能出现mysqli_real_connect ........ No such file的报错


五、其它安装

Memcached安装

分为服务端安装和客户端安装
1.服务端安装
方法1:

yum -y install memcached
/usr/bi/memcached -l 127.0.0.1 -p11211 -m 150 -u root

方法2:
memcached 依赖于libevent 库,因此我们需要先安装libevent.
假设将源码放在/usr/local/src

cd /usr/local/src
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz3 tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make && make install

安装memcached服务端

cd /usr/local/src
wget http://www.memcached.org/files/memcached-1.4.33.tar.gz3 tar zxvf memcached-1.4.33.tar.gz
cd memcached-1.4.335 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/loca/libevent
make && make install

如果出现g++ command not foundyum install gcc gcc-c++

2.客户端安装
客户端需依赖libmemcached

cd /usr/local/src
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz3 tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.185 ./configure --prefix=/usr/local/libmemcached
make && make install

安装php-fpmmemcached扩展

cd /usr/local/src
wget http://pecl.php.net/get/memcached-2.2.0.tgz3 tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0

到了这一步,我们要使用安装php时生成的 phpize 来生成 configure 配置文件

/usr/local/php/bin/phpize --with-php-config=/usr/local/php/bin/php-config
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make && make install

如果上面第一步phpize的时候出现
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable is set correctly and then rerun this script.

解决方案:

wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
./configure && make && make install
cd ../
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/
./configure && make && make install

如果第二步.configure编译的时候出现php_smart_str.h No such file or directory

git clone https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached/
git checkout php7
/usr/local/php/bin/phpize
./configure --disable-memcached-sasl --with-libmemcached-dir=/usr/local/libmemcached --with-php-config=/usr/local/php/bin/php-config


六、其它可能遇到的问题

NAT模式下无法上网

输入ifconfig没有ipv4地址的话 输入dhclient重新分配ip,并且要设定为开机自启,第一个要检查/etc/sysconfig/network-scripts/ifcfg-eth*文件BOOTPROTO=dhcp ONBOOT=yes,然后正确的话,就在/etc/rc.d/rc.local文件下追加

if config eth0 up 
dhclient eth0

(后面发现这个自启好像没用,每次都要手动执行dhclient,有空再看下)

桥接模式下无法上网

cd /etc/sysconfig/network-scripts/
vim ifcfg-enp0s3

将其改为动态获取ip,即为dhcp,然后设置开启自启

BOOTPROTO=dhcp
ONBOOT=yes

最后service network restart

出现windows无法ping通虚拟机

1.网络设置 注意要设置成桥接网卡

2.出现相互可以ping通,但是本地无法访问虚拟机的网站
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然后保存iptables-save


参考文章
1.Linux下安装php环境并且配置Nginx支持php-fpm模块
2.CentOS7安装MySQL
3.linux php安装memcached扩展

转载自原文链接, 如需删除请联系管理员。

原文链接:lnmp环境搭建(php7.2.4),转载请注明来源!

0