源码安装LNMP环境--PHP篇

PHP安装教程
PHP版本:7.2.10
资源下载:http://php.net/downloads.php

一、    安装依赖
yum -y install php-mcrypt 
yum -y install libmcrypt-devel 
yum -y install libxml2 
yum -y install libxml2-devel 
yum -y install openssl 
yum -y install openssl-devel 
yum -y install curl-devel 
yum -y install libjpeg-devel 
yum -y install libpng-devel 
yum -y install freetype-devel 
yum -y install libmcrypt-devel


二、    开始安装
将下载好的安装包通过FTP放到 /usr/local/src 目录下
①    解压并进入到解压目录中
cd /usr/local/src/
tar -zxvf php-7.2.10.tar.gz  (yum install -y tar)
cd php-7.2.10/
②    通过configure配置安装信息
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-mcrypt=/usr/include \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/var/run/mysql/mysql.sock \
--enable-mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir
③    编译安装
make && make install
④    安装成功信息
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
Installing PHP CLI binary:        /usr/local/php7/bin/
Installing PHP CLI man page:      /usr/local/php7/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7/sbin/
Installing PHP FPM defconfig:     /usr/local/php7/etc/
Installing PHP FPM man page:      /usr/local/php7/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7/bin/
Installing phpdbg man page:       /usr/local/php7/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7/bin/
Installing PHP CGI man page:      /usr/local/php7/php/man/man1/
Installing build environment:     /usr/local/php7/lib/php/build/
Installing header files:          /usr/local/php7/include/php/
Installing helper programs:       /usr/local/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php7/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php7/etc/pear.conf
You may want to add: /usr/local/php7/lib/php to your php.ini include_path
/usr/local/src/php-7.2.10/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/

三、配置PHP文件
①    拷贝PHP配置文件
cp php.ini-production /usr/local/php7/etc/php.ini
②    编辑PHP配置文件
vim /usr/local/php7/etc/php.ini
③    输入以下内容
zend_extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/opcache.so
解释:
开启PHP性能加速。
Opcache是一种通过将解析的PHP脚本预编译的字节码存放在共享内存中来避免每次加载和解析PHP脚本的开销,解析器可以直接从共享内存读取已经缓存的字节码,从而大大提高PHP的执行效率
④    拷贝php-fpm.conf文件
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
⑤    编辑php-fpm.conf
vim /usr/local/php7/etc/php-fpm.conf
⑥    输入以下内容并保存
pid = /var/run/php7/php-fpm.pid
⑦    拷贝www.conf文件(www.conf会加载在php-fpm.conf中)
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
⑧    编辑www.conf文件
vim /usr/local/php7/etc/php-fpm.d/www.conf
修改
listen = 127.0.0.1:9000
这里的listen要跟Nginx中的代理端口要一致,也可以都改成sock地址

四、    服务配置
①    创建并编辑文件php-fpm.service
vim /usr/lib/systemd/system/php-fpm.service
②    输入一下内容
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=/var/run/php7/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target

③    启动php-fpm
systemctl start php-fpm.service
④    添加到开机启动
systemctl enable php-fpm.service

五、    优化设置
①    修改 php.ini 文件 设置 expose_php = Off
vim /usr/local/php7/etc/php.ini
找到 expose_php = On
改为 expose_php = Off
解释:
为防止黑客获取服务器中php版本的信息,可以通过上面配置关闭该信息。telnet www.12345.com 80

备注
systemctl指令
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务
 

你可能感兴趣的:(后端,PHP,Linux)