centos7安装并配置nginx+php
-
安装nginx
yum install nginx
-
设置nginx开启起动
systemctl start nginx
- 测试
访问http://你的域名或IP/
-
查看nginx安装位置
whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
-
安装php
[不用这种]yum install php php-mysql php-fpm
安装过程中经常会见到如下问题:
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18(libmysqlclient_18)(64bit)
解决方法:
把php-mysql换成php-mysqlnd
即执行yum install php php-mysqlnd php-fpm php-gd php-mbstring
-
php-mbstring如果不安装,mb_substr字符串截取等无法用;
-
配置php处理器
vim /etc/php.ini
查找cgi.fix_pathinfo
将 ;cgi.fix_pathinfo=1改为cgi.fix_pathinfo=0 -
配置www.conf
vim /etc/php-fpm.d/www.conf
将
user = nobody
group = nobody
改为
user = nginx
group = nginx
前提是已经创建了nginx用户和nginx组。 -
起动php-fpm
systemctl start php-fpm
-
设置php-fpm开机启动
systemctl enable php-fpm
-
配置nginx
打开/etc/nginx/conf.d/default.conf,如果不存在则创建
粘贴
server {
listen 80;
server_name server_domain_name_or_IP;note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;location / {
location ~ .php$ {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
try_files $uri =404;
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
} -
重起nginx
systemctl restart nginx
-
测试php
创建/usr/share/nginx/html/index.php/usr/share/nginx/html/info.php
输入以下内容:
访问http://你的IP/index.php,正常情况下会出现
安装gd扩展库
yum install php-gd
1、如果HTML中套用PHP无法解析,如下操作:
打开/etc/php-fpm-d/www.conf,将分号去掉
;security.limit_extensions = .php .php3 .php4 .php5
改为:
security.limit_extensions = .php .php3 .php4 .php5
2、开启短标签
/etc/php.ini
查找short_open_tag改为
short_open_tag = On
3、出现PHP message: PHP Warning: date(): It is not safe to rely on the system's timezone settings.
说明时区设置有问题
/etc/php.ini
查找 date.timezone 改为
date.timezone = PRC
然后重启php-fpm服务
service php-fpm restart