前段时间我写了一篇文章:亚马逊AWS服务器CentOS/Linux系统Shell安装Nginx及配置自启动
今天有时间在整理一下亚马逊 AWS Linux 服务器安装 PHP 及配置自启动,话不多说。步骤如下:
亚马逊 AWS 服务器 Linux 安装PHP:
1、安装 PHP 需要的常用库:
yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
2、下载 PHP 安装包
cd /usr/local/src wget https://www.php.net/distributions/php-7.3.8.tar.gz tar -zxvf php-7.3.8.tar.gz
注意:我这里执行 tar 解压命令时报错,提示如下:
-bash: $'\342\200\213tar': command not found
看上去没什么,可能直接复制会有多余的空格或者字符,尝试手打就可以正常解压了。
3、配置
进入解压后的文件夹:
cd php-7.3.8
新建一个安装 PHP 的文件夹:
mkdir /usr/local/php
配置 configure:
./configure --prefix=/usr/local/php \ --with-mysql=mysqlnd \ --enable-mysqlnd \ --with-gd \ --enable-gd-jis-conv \ --enable-fpm
说明:
--prefix=
安装目录
--with-
使用包名称[=包目录]
--enable-
需要激活的功能
4、安装
make && make install
安装信息如下:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/ Installing PHP CLI binary: /usr/local/php/bin/ Installing PHP CLI man page: /usr/local/php/php/man/man1/ Installing PHP FPM binary: /usr/local/php/sbin/ Installing PHP FPM defconfig: /usr/local/php/etc/ Installing PHP FPM man page: /usr/local/php/php/man/man8/ Installing PHP FPM status page: /usr/local/php/php/php/fpm/ Installing phpdbg binary: /usr/local/php/bin/ Installing phpdbg man page: /usr/local/php/php/man/man1/ Installing PHP CGI binary: /usr/local/php/bin/ Installing PHP CGI man page: /usr/local/php/php/man/man1/ Installing build environment: /usr/local/php/lib/php/build/ Installing header files: /usr/local/php/include/php/ Installing helper programs: /usr/local/php/bin/ program: phpize program: php-config Installing man pages: /usr/local/php/php/man/man1/ page: phpize.1 page: php-config.1 Installing PEAR environment: /usr/local/php/lib/php/ [PEAR] Archive_Tar - installed: 1.4.7 [PEAR] Console_Getopt - installed: 1.4.2 [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.4.3 [PEAR] PEAR - installed: 1.10.9 Wrote PEAR system config file at: /usr/local/php/etc/pear.conf You may want to add: /usr/local/php/lib/php to your php.ini include_path /usr/local/src/php-7.3.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin ln -s -f phar.phar /usr/local/php/bin/phar Installing PDO headers: /usr/local/php/include/php/ext/pdo/
5、添加环境变量
vim /etc/profile
在末尾加入:
export PATH=$PATH:/usr/local/php/bin
保存修改后,使用 source
命令重新加载配置文件:
source /etc/profile
查看环境变量:
echo $PATH
查看 PHP 版本:
php -v
输出信息:
PHP 7.3.8 (cli) (built: Aug 3 2019 16:13:52) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.8, Copyright (c) 1998-2018 Zend Technologies
6、配置 php-fpm
cd /usr/local/php/etc cp php-fpm.conf.default php-fpm.conf cd php-fpm.d/ cp www.conf.default www.conf
使用 vim 命令对 php-fpm.conf 的内容进行如下修改:
pid = run/php-fpm.pid user = www group = www
其他配置可根据需求进行修改,比如:
pm.max_children
(php-fpm 能启动的子进程的最大数量)
pm.start_servers
(php启动时,开启的子进程的数量)
pm.min_spare_servers
(动态方式空闲状态下的最小php-fpm进程数量)
pm.max_spare_servers
(动态方式空闲状态下的最大php-fpm进程数量)等。
7、启动 php-fpm
/usr/local/php/sbin/php-fpm
可以通过 ps aux | grep php
查看 PHP 进程。
8、添加到开机启动
拷贝 php-fpm 脚本至 /etc/init.d 目录:
cp /usr/local/src/php-7.3.8/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
我的 php-fpm 代码是:仅供参考!
#! /bin/sh ### BEGIN INIT INFO # Provides: php-fpm # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts php-fpm # Description: starts the PHP FastCGI Process Manager daemon ### END INIT INFO prefix=/usr/local/php exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=${prefix}/var/run/php-fpm.pid php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID" wait_for_pid () { try=0 while test $try -lt 35 ; do case "$1" in 'created') if [ -f "$2" ] ; then try='' break fi ;; 'removed') if [ ! -f "$2" ] ; then try='' break fi ;; esac echo -n . try=`expr $try + 1` sleep 1 done } case "$1" in start) echo -n "Starting php-fpm " $php_fpm_BIN --daemonize $php_opts if [ "$?" != 0 ] ; then echo " failed" exit 1 fi wait_for_pid created $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Gracefully shutting down php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -QUIT `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if [ ! -r $php_fpm_PID ] ; then echo "php-fpm is stopped" exit 0 fi PID=`cat $php_fpm_PID` if ps -p $PID | grep -q $PID; then echo "php-fpm (pid $PID) is running..." else echo "php-fpm dead but pid file exists" fi ;; force-quit) echo -n "Terminating php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -TERM `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $0 stop $0 start ;; reload) echo -n "Reload service php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -USR2 `cat $php_fpm_PID` echo " done" ;; configtest) $php_fpm_BIN -t ;; *) echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}" exit 1 ;; esac
添加执行权限:
chmod 775 /etc/init.d/php-fpm
加入服务:
chkconfig --add php-fpm
开机自启:
chkconfig php-fpm on
常用命令:
service php-fpm start
启动
service php-fpm stop
停止
service php-fpm restart
重启
9、设置访问解析 PHP 文件:
找到 Nginx 配置文件,以我的为例 /usr/local/webserver/nginx/conf/nginx.conf
,放开 PHP 前面的 #
注释。
注意,这里的 root 应该放在 location 的外面, /scripts
改为 $document_root
,具体代码如下:
server { listen 80; server_name anli.pro; root /usr/local/webserver/nginx/html; location / { index index.php index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
大功告成啦~
常见问题解决:
报错:configure: error: libxml2 not found. Please check your libxml2 installation.
yum install -y libxml2-devel
报错:configure: error: Please reinstall the BZip2 distribution
yum install -y bzip2-devel
报错:configure: error: cURL version 7.15.5 or later is required to compile php with cURL support
yum install -y curl-devel
报错:configure: error: jpeglib.h not found.
yum install -y libjpeg-devel
报错:configure: error: png.h not found.
yum install -y libpng-devel
报错:configure: error: freetype-config not found.
yum install -y freetype-devel
报错:configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
yum install -y libxslt-devel
报错:configure: error: Please reinstall the libzip distribution
yum install -y libzip-devel
报错:checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
#先删除旧版本 yum remove -y libzip #下载编译安装 wget https://nih.at/libzip/libzip-1.2.0.tar.gz tar -zxvf libzip-1.2.0.tar.gz cd libzip-1.2.0 ./configure make && make install