Ubuntu源码Apache2安装支持PHP

前端之家收集整理的这篇文章主要介绍了Ubuntu源码Apache2安装支持PHP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

tips:make clean仅清除之前编译所生成可执行文件配置文件。make distclean要清除所有编译生成文件

先接上文

下载PHP源码包。

PHP下载地址。

官方网站

  1. #下载PHP源码
  2. dell@Ubuntu1601:~/PHP-7.1.3$ wget http://cn2.PHP.net/distributions/PHP-7.1.3.tar.gz
  3. #解压源码包
  4. dell@Ubuntu1601:~/PHP-7.1.3$ tar -zxvf httpd-2.4.25.tar.gz
  5. 切换到PHP代码目录
  6. #配置PHP安装选项
  7. dell@Ubuntu1601:~/PHP-7.1.3$ ./configure --prefix=/usr/local/PHP7 --with-apxs=/usr/local/apache2/bin/apxs --wi th-config-file-path=/usr/local/lib --with-MysqL

配置过程中可能会遇到xml2-config not found. Please check your libxml2 installation.错误,需要我们先安装XML2。

  1. checking whether to explicitly link against libgcc... no
  2. checking whether to enable short tags by default... yes
  3. checking whether to enable dmalloc... no
  4. checking whether to enable IPv6 support... yes
  5. checking whether to enable DTrace support... no
  6. checking how big to make fd sets... using system default
  7.  
  8. Configuring extensions
  9. checking size of long... (cached) 8
  10. checking size of int... (cached) 4
  11. checking for int32_t... yes
  12. checking for uint32_t... yes
  13. checking for sys/types.h... (cached) yes
  14. checking for inttypes.h... (cached) yes
  15. checking for stdint.h... (cached) yes
  16. checking for string.h... (cached) yes
  17. checking for stdlib.h... (cached) yes
  18. checking for strtoll... yes
  19. checking for atoll... yes
  20. checking for strftime... (cached) yes
  21. checking for gettimeofday... (cached) yes
  22. checking whether to enable LIBXML support... yes
  23. checking libxml2 install dir... no
  24. checking for xml2-config path...
  25. configure: error: xml2-config not found. Please check your libxml2 installation.
  26. dell@Ubuntu1601:~/PHP-7.1.3$
  27. #安装xml2
  28. dell@Ubuntu1601:~/PHP-7.1.3$ sudo apt install libxml2 libxml2-dev xml2

等待安装完成,重新配置PHP安装选项。

  1. dell@Ubuntu1601:~/PHP-7.1.3$ ./configure --prefix=/usr/local/PHP7 --with-apxs=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/lib --with-MysqL

以下是配置成功部分编译配置选项结果。

  1. checking for strip... strip
  2. checking if cc supports -fno-rtti -fno-exceptions... no
  3. checking for cc option to produce PIC... -fPIC
  4. checking if cc PIC flag -fPIC works... yes
  5. checking if cc static flag -static works... yes
  6. checking if cc supports -c -o file.o... yes
  7. checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
  8. checking whether -lc should be explicitly linked in... no
  9. checking dynamic linker characteristics... GNU/Linux ld.so
  10. checking how to hardcode library paths into programs... immediate
  11. checking whether stripping libraries is possible... yes
  12. checking if libtool supports shared libraries... yes
  13. checking whether to build shared libraries... yes
  14. checking whether to build static libraries... yes
  15.  
  16. creating libtool
  17. appending configuration tag "CXX" to libtool
  18.  
  19. Generating files
  20. configure: creating ./config.status
  21. creating main/internal_functions.c
  22. creating main/internal_functions_cli.c
  23. +--------------------------------------------------------------------+
  24. | License: |
  25. | This software is subject to the PHP License,available in this |
  26. | distribution in the file LICENSE. By continuing this installation |
  27. | process,you are bound by the terms of this license agreement. |
  28. | If you do not agree with the terms of this license,you must abort |
  29. | the installation process at this point. |
  30. +--------------------------------------------------------------------+
  31.  
  32. Thank you for using PHP.
  33.  
  34. config.status: creating PHP7.spec
  35. config.status: creating main/build-defs.h
  36. config.status: creating scripts/PHPize
  37. config.status: creating scripts/man1/PHPize.1
  38. config.status: creating scripts/PHP-config
  39. config.status: creating scripts/man1/PHP-config.1
  40. config.status: creating sapi/cli/PHP.1
  41. config.status: creating sapi/cgi/php-cgi.1
  42. config.status: creating ext/phar/phar.1
  43. config.status: creating ext/phar/phar.phar.1
  44. config.status: creating main/PHP_config.h
  45. config.status: executing default commands
  46. configure: WARNING: unrecognized options: --with-apxs,--with-MysqL

注意最后一行提示(警告)。

生成并安装。

  1. dell@Ubuntu1601:~/PHP-7.1.3$ make
  2. ...
  3. Generating phar.PHP
  4. Generating phar.phar
  5. PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
  6. invertedregexiterator.inc
  7. pharcommand.inc
  8. directorytreeiterator.inc
  9. clicommand.inc
  10. directorygraphiterator.inc
  11. phar.inc
  12.  
  13. Build complete.
  14. Don't forget to run 'make test'.
  15. ...
  16. dell@Ubuntu1601:~/PHP-7.1.3$ make test
  17. #根据机器配置,安装用时可能需要几分钟到几十分钟不等,请耐心等待安装完成。
  18. dell@Ubuntu1601:~/PHP-7.1.3$ sudo make install
  19. [sudo] dell 的密码:
  20. Installing shared extensions: /usr/local/PHP7/lib/PHP/extensions/no-debug-non-zts-20160303/
  21. Installing PHP CLI binary: /usr/local/PHP7/bin/
  22. Installing PHP CLI man page: /usr/local/PHP7/PHP/man/man1/
  23. Installing PHPdbg binary: /usr/local/PHP7/bin/
  24. Installing PHPdbg man page: /usr/local/PHP7/PHP/man/man1/
  25. Installing PHP CGI binary: /usr/local/PHP7/bin/
  26. Installing PHP CGI man page: /usr/local/PHP7/PHP/man/man1/
  27. Installing build environment: /usr/local/PHP7/lib/PHP/build/
  28. Installing header files: /usr/local/PHP7/include/PHP/
  29. Installing helper programs: /usr/local/PHP7/bin/
  30. program: PHPize
  31. program: PHP-config
  32. Installing man pages: /usr/local/PHP7/PHP/man/man1/
  33. page: PHPize.1
  34. page: PHP-config.1
  35. Installing PEAR environment: /usr/local/PHP7/lib/PHP/
  36. [PEAR] Archive_Tar - installed: 1.4.2
  37. [PEAR] Console_Getopt - installed: 1.4.1
  38. [PEAR] Structures_Graph- installed: 1.1.1
  39. [PEAR] XML_Util - installed: 1.4.2
  40. [PEAR] PEAR - installed: 1.10.3
  41. Wrote PEAR system config file at: /usr/local/PHP7/etc/pear.conf
  42. You may want to add: /usr/local/PHP7/lib/PHP to your PHP.ini include_path
  43. /home/dell/PHP-7.1.3/build/shtool install -c ext/phar/phar.phar /usr/local/PHP7/bin
  44. ln -s -f phar.phar /usr/local/PHP7/bin/phar
  45. Installing PDO headers: /usr/local/PHP7/include/PHP/ext/pdo/
  46. #查看当前目录文件
  47. dell@Ubuntu1601:~/PHP-7.1.3$ ls
  48. acinclude.m4 header NEWS README.TESTING2
  49. aclocal.m4 include pear README.UNIX-BUILD-SYSTEM
  50. appveyor INSTALL PHP7.spec README.WIN32-BUILD-SYSTEM
  51. build install-sh PHP7.spec.in run_002_tmp.fixture
  52. buildconf libs PHP.gif run-tests.PHP
  53. buildconf.bat libtool PHP.ini-development sapi
  54. CODING_STANDARDS LICENSE PHP.ini-production scripts
  55. config.guess ltmain.sh README.EXT_SKEL server-tests-config.PHP
  56. config.log main README.GIT-RULES server-tests.PHP
  57. config.nice makedist README.input_filter sessions
  58. config.status Makefile README.MAILINGLIST_RULES snapshot
  59. config.sub Makefile.frag README.md stamp-h.in
  60. configure Makefile.fragments README.namespaces stub.c
  61. configure.in Makefile.gcov README.NEW-OUTPUT-API tests
  62. CONTRIBUTING.md Makefile.global README.PARAMETER_PARSING_API travis
  63. CREDITS Makefile.objects README.REDIST.BINS TSRM
  64. ext makerpm README.RELEASE_PROCESS UPGRADING
  65. EXTENSIONS missing README.SELF-CONTAINED-EXTENSIONS UPGRADING.INTERNALS
  66. footer mkinstalldirs README.STREAMS vcsclean
  67. generated_lists modules README.SUBMITTING_PATCH win32
  68. genfiles netware README.TESTING Zend
  69. dell@Ubuntu1601:~/PHP-7.1.3$ sudo cp PHP.ini-development /usr/local/lib/PHP.ini
  70. dell@Ubuntu1601:~/PHP-7.1.3$ sudo cp /usr/local/apache2/conf/httpd.conf
  71. #修改前注意备份文件
  72. /usr/local/apache2/conf/httpd.conf.bak dell@Ubuntu1601:~/PHP-7.1.3$ sudo vim /usr/local/apache2/conf/httpd.conf
  73. #找到以下两行代码修改
  74. #修改完成结果如下
  75. # DirectoryIndex index.html index.PHP
  76. #设置apache的默认文件名次序
  77. #AddType application/x-httpd-PHP .PHP .phtml .PHP3 .inc
  78.  
  79. #AddType application/x-httpd-PHP-source .PHPs
  80. #设置PHP文件后缀
  81. dell@Ubuntu1601:~/PHP-7.1.3$ sudo /etc/init.d/apachectl restart

编辑过程可能会遇到下面代码。(PHP5.4以上没有)

  1. vi /usr/local/lib/PHP.ini
  2. #参考链接: http://PHP.net/manual/zh/security.globals.PHP
  3. #register-golbals = On

安装到这一步,PHP还无法正常解析。

通过参考网上链接,发现Apache没有加载libPHP*.so模块。

  1. #检查Apache模块加载情况
  2. dell@Ubuntu1601:~$ /etc/init.d/apachectl -M
  3. Loaded Modules:
  4. core_module (static)
  5. so_module (static)
  6. http_module (static)
  7. mpm_event_module (static)
  8. authn_file_module (shared)
  9. authn_core_module (shared)
  10. authz_host_module (shared)
  11. authz_groupfile_module (shared)
  12. authz_user_module (shared)
  13. authz_core_module (shared)
  14. access_compat_module (shared)
  15. auth_basic_module (shared)
  16. reqtimeout_module (shared)
  17. filter_module (shared)
  18. mime_module (shared)
  19. log_config_module (shared)
  20. env_module (shared)
  21. headers_module (shared)
  22. setenvif_module (shared)
  23. version_module (shared)
  24. unixd_module (shared)
  25. status_module (shared)
  26. autoindex_module (shared)
  27. dir_module (shared)
  28. alias_module (shared)
  29. #检查Apache配置
  30. dell@Ubuntu1601:~$ /etc/init.d/apachectl -t
  31. Syntax OK

接着尝试重新编译安装PHP

  1. dell@Ubuntu1601:~$ cd PHP-7.1.3/
  2. dell@Ubuntu1601:~/PHP-7.1.3$ ./configure --prefix=/usr/local/PHP7 --with-apxs2=/usr/local/apache2/bin/apxs --w ith-MysqL=/usr/local/MysqL --enable-ftp --with-freetype-dir=/usr/local/freetype --enable-mbstring --with-iconv
  3.  
  4. dell@Ubuntu1601:~/PHP-7.1.3$ make
  5. dell@Ubuntu1601:~/PHP-7.1.3$ sudo make install
  6. #部分报错信息
  7. Generating phar.PHP
  8. Generating phar.phar
  9. PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
  10. invertedregexiterator.inc
  11. pharcommand.inc
  12. directorytreeiterator.inc
  13. clicommand.inc
  14. directorygraphiterator.inc
  15. phar.inc
  16.  
  17. Build complete.
  18. Don't forget to run 'make test'.
  19.  
  20. Installing PHP SAPI module: apache2handler
  21. mkdir: cannot create directory '/usr/local/PHP7': Permission denied
  22. mkdir: cannot create directory '/usr/local/PHP7/bin': No such file or directory
  23. Makefile:147: recipe for target 'install-sapi' Failed
  24. make: [install-sapi] Error 1 (ignored)
  25. /usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apr//build-1/libtool' libPHP7.la /usr/local/apache2/modules
  26. /usr/local/apr//build-1/libtool --mode=install install libPHP7.la /usr/local/apache2/modules/
  27. libtool: install: install .libs/libPHP7.so /usr/local/apache2/modules/libPHP7.so
  28. install: cannot create regular file '/usr/local/apache2/modules/libPHP7.so': Permission denied
  29. apxs:Error: Command Failed with rc=65536
  30. .
  31. Makefile:147: recipe for target 'install-sapi' Failed
  32. make: *** [install-sapi] Error 1
  33. dell@Ubuntu1601:~/PHP-7.1.3$

推测需要重新安装PHP

tips:已经重新安装测试机系统。

最新进展:

  1. #按两下Tab键
  2. #可以看到没有--with-apxs选项
  3. dell@Dell:~/PHP-7.1.3$ ./configure --prefix=/usr/local/PHP7.1 --with
  4. Display all 101 possibilities? (y or n)
  5. --with-adabas= --with-interbase= --with-pcre-jit
  6. --with-apxs2= --with-iodbc= --with-pcre-regex=
  7. --with-birdstep= --with-jpeg-dir= --with-pdo-dblib=
  8. --with-bz2= --with-kerberos= --with-pdo-firebird=
  9. --with-config-file-path= --with-layout= --with-pdo-MysqL=
  10. --with-config-file-scan-dir= --with-ldap= --with-pdo-oci=
  11. --with-curl= --with-ldap-sasl= --with-pdo-odbc=
  12. --with-custom-odbc= --with-libdir= --with-pdo-pgsql=
  13. --with-db1= --with-libedit= --with-pear=
  14. --with-db2= --with-libexpat-dir= --with-pgsql=
  15. --with-db3= --with-libmbfl= --with-pic
  16. --with-db4= --with-libxml-dir= --with-png-dir=
  17. --with-dbm= --with-libzip= --with-pspell=
  18. --with-dbmaker= --with-litespeed --with-qdbm=
  19. --with-empress= --with-mcrypt= --with-readline=
  20. --with-empress-bcs= --with-mhash= --with-recode=
  21. --with-enchant= --with-mm= --with-sapdb=
  22. --with-esoob= --with-MysqLi= --with-snmp=
  23. --with-EXTENSION= --with-MysqL-sock= --with-solid=
  24. --with-fpm-acl --with-ndbm= --with-system-ciphers
  25. --with-fpm-group= --with-oci8= --with-tags=
  26. --with-fpm-systemd --with-ODBCRouter= --with-tcadb=
  27. --with-fpm-user= --with-odbcver= --with-tidy=
  28. --with-freetype-dir= --with-onig= --with-tsrm-pth=
  29. --with-gd= --with-openssl= --with-tsrm-pthreads
  30. --with-gdbm= --with-openssl-dir= --with-tsrm-st
  31. --More--

经过两天辛苦排查,终于发现是PHP编译选项有问题。 具体表现在代码

  1. dell@Dell:~/PHP-7.1.3$ ./configure --prefix=/usr/local/PHP7 --with-apxs=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/lib --with-MysqL

--with-apxs选项少一个2(--with-apxs2),其他选项可选。

编译完成,检查http配置文件

  1. #部分代码
  2. dell@Dell:~$ cat /usr/local/apache2.4/conf/httpd.conf |more
  3. #LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
  4. #LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
  5. #LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
  6. #LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
  7. LoadModule unixd_module modules/mod_unixd.so
  8. #LoadModule dav_module modules/mod_dav.so
  9. LoadModule status_module modules/mod_status.so
  10. LoadModule autoindex_module modules/mod_autoindex.so
  11. #LoadModule info_module modules/mod_info.so
  12. #LoadModule cgid_module modules/mod_cgid.so
  13. #LoadModule dav_fs_module modules/mod_dav_fs.so
  14. #LoadModule vhost_alias_module modules/mod_vhost_alias.so
  15. #LoadModule negotiation_module modules/mod_negotiation.so
  16. LoadModule dir_module modules/mod_dir.so
  17. #LoadModule actions_module modules/mod_actions.so
  18. #LoadModule speling_module modules/mod_speling.so
  19. #LoadModule userdir_module modules/mod_userdir.so
  20. LoadModule alias_module modules/mod_alias.so
  21. #LoadModule rewrite_module modules/mod_rewrite.so
  22. LoadModule PHP7_module modules/libPHP7.so
  23.  
  24. <IfModule unixd_module>
  25. #
  26. # If you wish httpd to run as a different user or group,you must run
  27. # httpd as root initially and it will switch.
  28. #
  29. --更多--

检查代码发现PHP7_module已经加载。

PHP安装完成后执行

  1. dell@dell-OptiPlex-390:~/PHP-7.1.3$ sudo cp PHP.ini-production /usr/local/lib/PHP.ini

编译Apache配置文件

  1. dell@DELL:~/SoftwareSource/PHP-7.1.4$ sudo vim /usr/local/apache2.2/conf/httpd.conf
  2. #添加index.PHP
  3. 167 <IfModule dir_module>
  4. 168 DirectoryIndex index.html index.PHP
  5. #添加支持PHP
  6. 169 </IfModule>
  7. 318 AddType application/x-compress .Z
  8. 319 AddType application/x-gzip .gz .tgz
  9. AddType application/x-httpd-PHP .PHP .phtml
  10. AddType application/x-httpd-PHP-source .PHPs

保存修改新建PHP测试页面

  1. #编辑PHPinfo.PHP
  2. dell[@Dell](https://my.oschina.net/u/725578):~$ sudo vim /usr/local/apache2.4/htdocs/PHPinfo.PHP
  3. #PHPinfo.PHP
  4. <?PHP
  5. PHPinfo();
  6. ?>

保存文件,浏览器输入输入链接。例如输入本机IP地址

以下是编译成功与浏览器运行截图。

编译过程中还可能遇到

  1. dell[@Dell](https://my.oschina.net/u/725578):~/PHP-7.1.3$ make install
  2. Installing PHP SAPI module: apache2handler
  3. mkdir: cannot create directory '/usr/local/PHP7.1': Permission denied
  4. mkdir: cannot create directory '/usr/local/PHP7.1/bin': No such file or directory
  5. Makefile:146: recipe for target 'install-sapi' Failed
  6. make: [install-sapi] Error 1 (ignored)
  7. /usr/local/apache2.4/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libPHP7.la /usr/local/apache2.4/modules
  8. /usr/local/apr/build-1/libtool --mode=install install libPHP7.la /usr/local/apache2.4/modules/
  9. libtool: install: install .libs/libPHP7.so /usr/local/apache2.4/modules/libPHP7.so
  10. install: cannot create regular file '/usr/local/apache2.4/modules/libPHP7.so': Permission denied
  11. apxs:Error: Command Failed with rc=65536
  12. .
  13. Makefile:146: recipe for target 'install-sapi' Failed
  14. make: *** [install-sapi] Error 1

错误,请以root权限重新运行安装程序。

  1. dell[@Dell](https://my.oschina.net/u/725578):~/PHP-7.1.3$ sudo make install
  2. Installing PHP SAPI module: apache2handler
  3. /usr/local/apache2.4/build/instdso.sh SH_LIBTOOL='/usr/local/apr/build-1/libtool' libPHP7.la /usr/local/apache2.4/modules
  4. /usr/local/apr/build-1/libtool --mode=install install libPHP7.la /usr/local/apache2.4/modules/
  5. libtool: install: install .libs/libPHP7.so /usr/local/apache2.4/modules/libPHP7.so
  6. libtool: install: install .libs/libPHP7.lai /usr/local/apache2.4/modules/libPHP7.la
  7. libtool: install: warning: remember to run `libtool --finish /home/dell/PHP-7.1.3/libs'
  8. chmod 755 /usr/local/apache2.4/modules/libPHP7.so
  9. [activating module `PHP7' in /usr/local/apache2.4/conf/httpd.conf]
  10. Installing shared extensions: /usr/local/PHP7.1/lib/PHP/extensions/no-debug-zts-20160303/
  11. Installing PHP CLI binary: /usr/local/PHP7.1/bin/
  12. Installing PHP CLI man page: /usr/local/PHP7.1/PHP/man/man1/
  13. Installing PHPdbg binary: /usr/local/PHP7.1/bin/
  14. Installing PHPdbg man page: /usr/local/PHP7.1/PHP/man/man1/
  15. Installing PHP CGI binary: /usr/local/PHP7.1/bin/
  16. Installing PHP CGI man page: /usr/local/PHP7.1/PHP/man/man1/
  17. Installing build environment: /usr/local/PHP7.1/lib/PHP/build/
  18. Installing header files: /usr/local/PHP7.1/include/PHP/
  19. Installing helper programs: /usr/local/PHP7.1/bin/
  20. program: PHPize
  21. program: PHP-config
  22. Installing man pages: /usr/local/PHP7.1/PHP/man/man1/
  23. page: PHPize.1
  24. page: PHP-config.1
  25. Installing PEAR environment: /usr/local/PHP7.1/lib/PHP/
  26. [PEAR] Archive_Tar - installed: 1.4.2
  27. [PEAR] Console_Getopt - installed: 1.4.1
  28. [PEAR] Structures_Graph- installed: 1.1.1
  29. [PEAR] XML_Util - installed: 1.4.2
  30. [PEAR] PEAR - installed: 1.10.3
  31. Wrote PEAR system config file at: /usr/local/PHP7.1/etc/pear.conf
  32. You may want to add: /usr/local/PHP7.1/lib/PHP to your PHP.ini include_path
  33. /home/dell/PHP-7.1.3/build/shtool install -c ext/phar/phar.phar /usr/local/PHP7.1/bin
  34. ln -s -f phar.phar /usr/local/PHP7.1/bin/phar
  35. Installing PDO headers: /usr/local/PHP7.1/include/PHP/ext/pdo/

猜你在找的Ubuntu相关文章