配置php服务

aiofo2022-08-15  276

PHP(Hypertxt Preprocessor,超文本预处理器)是一种通用的开源脚本语言,发明于1995年,它吸取了C语言、Java语言及Perl语言的很多优点,具有开源、免费、快捷、跨平台性强、效率高等优良特性,是目前Web开发领域最常用的语言之一。本书的配套站点就是基于PHP语言编写的。

使用源码包的方式编译安装PHP语言环境其实并不复杂,难点在于解决PHP的程序包和其他软件的依赖关系。为此需要先安装部署将近十个用于搭建网站页面的软件程序包,然后才能正式安装PHP程序。

yasm源码包是一款常见的开源汇编器,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe nginx-1.6.0]# cd ..

[root@linuxprobe src]# tar zxvf yasm-1.2.0.tar.gz

[root@linuxprobe src]# cd yasm-1.2.0

[root@linuxprobe yasm-1.2.0]# ./configure

[root@linuxprobe yasm-1.2.0]# make

[root@linuxprobe yasm-1.2.0]# make install libmcrypt源码包是用于加密算法的扩展库程序,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe yasm-1.2.0]# cd ..

[root@linuxprobe src]# tar zxvf libmcrypt-2.5.8.tar.gz

[root@linuxprobe src]# cd libmcrypt-2.5.8

[root@linuxprobe libmcrypt-2.5.8]# ./configure

[root@linuxprobe libmcrypt-2.5.8]# make

[root@linuxprobe libmcrypt-2.5.8]# make install libvpx

源码包是用于提供视频编码器的服务程序,其解压、编译、安装过程中生成的输出信息均已省略。相信会有很多粗心的读者顺手使用了tar命令的xzvf参数,但如果仔细观察就会发现libvpx源码包的后缀是.tar.bz2,即表示使用bzip2格式进行的压缩,因此正确的解压参数应该是xjvf:

[root@linuxprobe libmcrypt-2.5.8]# cd ..

[root@linuxprobe src]# tar xjvf libvpx-v1.3.0.tar.bz2

[root@linuxprobe src]# cd libvpx-v1.3.0

[root@linuxprobe libvpx-v1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9

[root@linuxprobe libvpx-v1.3.0]# make

[root@linuxprobe libvpx-v1.3.0]# make install tiff源码包是用于提供标签图像文件格式的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe libvpx-v1.3.0]# cd ..

[root@linuxprobe src]# tar zxvf tiff-4.0.3.tar.gz

[root@linuxprobe src]# cd tiff-4.0.3

[root@linuxprobe tiff-4.0.3]# ./configure --prefix=/usr/local/tiff --enable-shared

[root@linuxprobe tiff-4.0.3]# make

[root@linuxprobe tiff-4.0.3]# make install libpng源码包是用于提供png图片格式支持函数库的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe tiff-4.0.3]# cd ..

[root@linuxprobe src]# tar zxvf libpng-1.6.12.tar.gz

[root@linuxprobe src]# cd libpng-1.6.12

[root@linuxprobe libpng-1.6.12]# ./configure --prefix=/usr/local/libpng --enable-shared

[root@linuxprobe libpng-1.6.12]# make

[root@linuxprobe libpng-1.6.12]# make install freetype源码包是用于提供字体支持引擎的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe libpng-1.6.12]# cd ..

[root@linuxprobe src]# tar zxvf freetype-2.5.3.tar.gz

[root@linuxprobe src]# cd freetype-2.5.3

[root@linuxprobe freetype-2.5.3]# ./configure --prefix=/usr/local/freetype --enable-shared

[root@linuxprobe freetype-2.5.3]# make

[root@linuxprobe freetype-2.5.3]# make install jpeg源码包是用于提供jpeg图片格式支持函数库的服务程序,其解压、编译、安装过程中生成的输出信息均已省略:

[root@linuxprobe freetype-2.5.3]# cd ..

[root@linuxprobe src]# tar zxvf jpegsrc.v9a.tar.gz

[root@linuxprobe src]# cd jpeg-9a

[root@linuxprobe jpeg-9a]# ./configure --prefix=/usr/local/jpeg --enable-shared

[root@linuxprobe jpeg-9a]# make

[root@linuxprobe jpeg-9a]# make install libgd源码包是用于提供图形处理的服务程序,其解压、编译、安装过程中生成的输出信息均已省略。

在编译libgd源码包时,请记得写入的是jpeg、libpng、freetype、tiff、libvpx等服务程序在系统中的安装路径,即在上面安装过程中使用--prefix参数指定的目录路径:

[root@linuxprobe jpeg-9a]# cd ..

[root@linuxprobe src]# tar zxvf libgd-2.1.0.tar.gz

[root@linuxprobe src]# cd libgd-2.1.0

[root@linuxprobe libgd-2.1.0]# ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx

[root@linuxprobe libgd-2.1.0]# make

[root@linuxprobe libgd-2.1.0]# make install t1lib源码包是用于提供图片生成函数库的服务程序,其解压、编译、安装过程中生成的输出信息均已省略。安装后把/usr/lib64目录中的函数文件链接到/usr/lib目录中,以便系统能够顺利调取到函数文件:

[root@linuxprobe cd libgd-2.1.0]# cd ..

[root@linuxprobe src]# tar zxvf t1lib-5.1.2.tar.gz

[root@linuxprobe src]# cd t1lib-5.1.2

[root@linuxprobe t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared

[root@linuxprobe t1lib-5.1.2]# make

[root@linuxprobe t1lib-5.1.2]# make install

[root@linuxprobe t1lib-5.1.2]# ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so

[root@linuxprobe t1lib-5.1.2]# cp -frp /usr/lib64/libXpm.so* /usr/lib/ 此时终于把编译php服务源码包的相关软件包都已经安装部署妥当了。在开始编译php源码包之前,先定义一个名为LD_LIBRARY_PATH的全局环境变量,该环境变量的作用是帮助系统找到指定的动态链接库文件,这些文件是编译php服务源码包的必须元素之一。编译php服务源码包时,除了定义要安装到的目录以外,还需要依次定义配置php服务程序配置文件的保存目录、MySQL数据库服务程序所在目录、MySQL数据库服务程序配置文件所在目录,以及libpng、jpeg、freetype、libvpx、zlib、t1lib等服务程序的安装目录路径,并通过参数启动php服务程序的诸多默认功能:

[root@linuxprobe t1lib-5.1.2]# cd ..

[root@linuxprobe src]# tar -zvxf php-5.5.14.tar.gz

[root@linuxprobe src]# cd php-5.5.14

[root@linuxprobe php-5.5.14]# export LD_LIBRARY_PATH=/usr/local/libgd/lib

[root@linuxprobe php-5.5.14]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype

[root@linuxprobe php-5.5.14]# make

[root@linuxprobe php-5.5.14]# make install 在php源码包程序安装完成后,需要删除当前默认的配置文件,然后将php服务程序目录中相应的配置文件复制过来:

[root@linuxprobe php-5.5.14]# rm -rf /etc/php.ini

[root@linuxprobe php-5.5.14]# ln -s /usr/local/php/etc/php.ini /etc/php.ini

[root@linuxprobe php-5.5.14]# cp php.ini-production /usr/local/php/etc/php.ini

[root@linuxprobe php-5.5.14]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@linuxprobe php-5.5.14]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf php-fpm.conf是php服务程序重要的配置文件之一,我们需要启用该配置文件中第25行左右的pid文件保存目录,然后分别将第148和149行的user与group参数分别修改为www账户和用户组名称:

[root@linuxprobe php-5.5.14]# vim /usr/local/php/etc/php-fpm.conf

1 ;;;;;;;;;;;;;;;;;;;;;

2 ; FPM Configuration ;

3 ;;;;;;;;;;;;;;;;;;;;;

4

5 ; All relative paths in this configuration file are relative to PHP's instal l

6 ; prefix (/usr/local/php). This prefix can be dynamically changed by using t he

7 ; '-p' argument from the command line.

8

9 ; Include one or more files. If glob(3) exists, it is used to include a bunc h of

10 ; files from a glob(3) pattern. This directive can be used everywhere in the

11 ; file.

12 ; Relative path can also be used. They will be prefixed by:

13 ; - the global prefix if it's been set (-p argument)

14 ; - /usr/local/php otherwise

15 ;include=etc/fpm.d/*.conf

16

17 ;;;;;;;;;;;;;;;;;;

18 ; Global Options ;

19 ;;;;;;;;;;;;;;;;;;

20

21 [global]

22 ; Pid file

23 ; Note: the default prefix is /usr/local/php/var

24 ; Default Value: none

25 pid = run/php-fpm.pid

26 ………………省略部分输出信息………………

145 ; Unix user/group of processes

146 ; Note: The user is mandatory. If the group is not set, the default user's g roup

147 ; will be used.

148 user = www

149 group = www

150 ………………省略部分输出信息………………

配置妥当后便可把用于管理php服务的脚本文件复制到/etc/rc.d/init.d中了。为了能够执行脚本,请记得为脚本赋予755权限。最后把php-fpm服务程序加入到开机启动项中:

[root@linuxprobe php-5.5.14]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

[root@linuxprobe php-5.5.14]# chmod 755 /etc/rc.d/init.d/php-fpm

[root@linuxprobe php-5.5.14]# chkconfig php-fpm on

由于php服务程序的配置参数直接会影响到Web服务服务的运行环境,因此,如果默认开启了一些不必要且高危的功能(如允许用户在网页中执行Linux命令),则会降低网站被入侵的难度,入侵人员甚至可以拿到整台Web服务器的管理权限。因此我们需要编辑php.ini配置文件,在305行的disable_functions参数后面追加上要禁止的功能。下面的禁用功能名单是刘遄老师依据网站运行的经验而定制的,不见得适合每个生产环境,建议大家在此基础上根据自身工作需求酌情删减:

[root@linuxprobe php-5.5.14]# vim /usr/local/php/etc/php.ini ………………省略部分输出信息………………

300

301 ; This directive allows you to disable certain functions for security reasons.

302 ; It receives a comma-delimited list of function names. This directive is

303 ; NOT affected by whether Safe Mode is turned On or Off.

304 ; http://php.net/disable-functions 

305 disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restor e,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,g etservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,po six_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posixgetpwnam,posix getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posixsetgid,posix setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

306 ………………省略部分输出信息………………

这样就把php服务程序配置妥当了。最后,还需要编辑Nginx服务程序的主配置文件,把第2行的井号(#)删除,然后在后面写上负责运行Nginx服务程序的账户名称和用户组名称;在第45行的index参数后面写上网站的首页名称。最后是将第65~71行参数前的井号(#)删除来启用参数,主要是修改第69行的脚本名称路径参数,其中$document_root变量即为网站信息存储的根目录路径,若没有设置该变量,则Nginx服务程序无法找到网站信息,因此会提示“404页面未找到”的报错信息。在确认参数信息填写正确后便可重启Nginx服务与php-fpm服务。

[root@linuxprobe php-5.5.14]# vim /usr/local/nginx/conf/nginx.conf

1

2 user www www;

3 worker_processes 1;

4

5 #error_log logs/error.log;

6 #error_log logs/error.log notice;

7 #error_log logs/error.log info;

8

9 #pid logs/nginx.pid;

10

11 ………………省略部分输出信息………………

40

41 #access_log logs/host.access.log main;

42

43 location / {

44 root html;

45 index index.html index.htm index.php;

46 }

47 ………………省略部分输出信息………………

62

63 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

64

65 location ~ .php$ {

66 root html;

67 fastcgi_pass 127.0.0.1:9000;

68 fastcgi_index index.php;

69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

70 include fastcgi_params;

71 }

72 ………………省略部分输出信息………………

[root@linuxprobe php-5.5.14]# systemctl restart nginx

[root@linuxprobe php-5.5.14]# systemctl restart php-fpm

至此,LNMP动态网站环境架构的配置实验全部结束。

转载请注明原文地址:https://www.aiofo.com/read-1461.html