阿里云轻量服务器安装PHP环境
Linux安装PHP-FPM
之前用XAMPP(是一个功能强大的建站集成软件包,包含Apache+MySQL+PHP)安装过,Apache+MySQL+PHP安装成功,但是在启动时MySQL一直无法启动,所以PHP环境一直未搭建成功。后来改用PHP-FPM方法成功运行php文件,环境搭建成功。(注:以下涉及Apache的安装可能无法成功,建议先安装好Apache)
1、安装编译环境
yum -y install epel-release
yum -y install gcc automake autoconf libtool make gcc-c++ glibc 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 libmcrypt mcrypt mhash php-mcrypt
2、下载php版本包
php官方地址:https://www.php.net/releases/
本次安装环境的版本包为php5.6
wget http://cn2.php.net/distributions/php-5.6.24.tar.gz
tar zvxf php-5.6.24.tar.gz
cd php-5.6.24
3、编译
php编译过程中,如果要php支持相应的功能,需要先安装对应的组件,然后再编译。
./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli
make && make install
4、配置文件
复制配置文件,对其中一些代码进行修改,可根据需要开启php中的功能
cp php.ini-development /usr/local/php/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-fpm.conf配置文件,使用www用户和www用户组运行,大概在第150行
vim /usr/local/php/etc/php-fpm.conf
#修改为以下
user = www
group = www
修改php.ini,根据需求开启需要的php功能
vim /usr/local/php/php.ini
#############################
display_errors = On
display_startup_errors = On
error_prepend_string = "<br><font color=#ff0000>"
error_append_string = "</font><br><br>"
fastcgi.impersonate = 1
date.timezone = asia/Shanghai
extension=php_mysql.dll
extension=php_gd2.dll
extension=php_mbstring.dll
建议通过FZ来打开php.ini文件进行修改
5、运行
/usr/local/bin/php-fpm
如果报如下错误
cannot get uid for user ‘www’
则在当前路径输入如下命令
groupadd www
useradd -g www www
否则继续执行如下命令
#查看是否运行
netstat -anop | grep php
出现以下界面表示正常运行:
6、nginx添加PHP支持
修改NGINX配置文件,nginx.conf其中server段增加如下配置,注意标记内容配置,否则会出现No input file specified.错误.
(1)查找nginx.conf文件路径:
先找出nginx可执行文件的路径
ps aux|grep nginx
查看nginx配置文件路径
使用nginx的 -t 参数进行配置检查,即可知道实际调用的配置文件路径及是否调用有效。/usr/sbin/nginx -t
(2)将nginx.conf文件中的serverx修改如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /opt/lampp/htdocs;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php$ {
root /opt/lampp/htdocs;
include fastcgi_params; #引入fastcgi的配置文件
fastcgi_pass 127.0.0.1:9000; #设置php fastcgi进程监听的IP地址和端口
fastcgi_index index.php; #设置首页文件
fastcgi_param SCRIPT_FILENAME /opt/lampp/htdocs$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
7、Apache添加PHP支持
(1)查看apache配置文件httpd.conf的路径:
httpd -V
(2)在httpd.conf添加以下配置:
LoadModule php5_module modules/libphp5.so
##########################################
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
8、将php项目文件放在/opt/lampp/htdocs路径下
9、使用域名或网址访问时,就可以显示出来了!
10、参考博客
Linux安装PHP-FPM
Linux下如何查找nginx配置文件的位置
https://www.cnblogs.com/zongyl/p/5913911.html