centos7中安装PHP和nginx
Created at 2019-05-18 Updated at 2019-08-15 Category 搭建服务
centos7中安装PHP和nginx
在centos7中安装nginx,有许多依赖的环境
gcc,gcc-c++安装,安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum -y install gcc gcc-c++
pcre,pcre-devel的安装,PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
yum -y install pcre pcre-devel
zlib,zlib-devel的安装,zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum -y install zlib zlib-devel
openssl,OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum -y install openssl openssl-devel
在centos7中源码安装nginx
1,在网上把nginx源码包复制链接,下载下来
wget 链接(在/usr/local/src/)
tar -xzvf nginx-1.12.2.tar.gz
2,建立nginx用户及用户组
useradd nginx -s /sbin/nologin -M
3,cd /usr/local/src/nginx-1.12.2
mkdir -p /data/server (用来装服务的目录)
./configure
–prefix=/data/server/nginx
–sbin-path=/data/server/nginx/sbin/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock
–user=nginx
–group=nginx
–with-http_ssl_module
–with-http_flv_module
–with-http_stub_status_module
–with-http_gzip_static_module
–http-client-body-temp-path=/var/tmp/nginx/client/
–http-proxy-temp-path=/var/tmp/nginx/proxy/
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
–http-scgi-temp-path=/var/tmp/nginx/scgi
–with-pcre
make
make install
4,关闭防火墙,selinux
systemctl stop firewalld
setenforce 0
5,启动nginx服务
/data/server/nginx/sbin/nginx
但这时并不能成功启动nginx,会报一下错误
[emerg] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory)
说的是没有这个文件或者目录
找到nginx的配置文件可 find / -name nginx.conf查找到后对nginx的配置文件进行编辑
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;(把#删除)
这时还是重启nginx服务还是不会成功,说/data/server/nginx/logs/error.log文件不存在
mkdir -p /data/server/nginx/logs/
touch /data/server/nginx/logs/error.log
6,查看nginx服务
ps aux |grep nginx
netstat -pantu |grep nginx/80
以上安装nginx即成功
访问页面即出现如上图所示页面
++++++++++++++++++++++++++++++++++
centos7中安装源码安装PHP
1,首先在安装PHP前需要很多依赖包
gcc,gcc-c++,bison,bison-devel,zlib-devel,libmcrypt-devel,mcrypt,mhash-devel, openssl-devel ,libxml2-devel, libcurl-devel, bzip2-devel ,readline-devel ,libedit-devel,sqlite-devel ,gd,gd-devel
yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel gd gd-devel
2,在网上找到PHP包的网址复制下来
wget http://cn2.php.net/distributions/php-5.6.0.tar.xz
tar -xvf php-5.6.0.tar
3,创建用户及用户组
groupadd www
useradd -g www -s /sbin/nologin -M www
4,对PHP进行编译安装
cd /usr/local/src/php-5.6.0
./configure –prefix=/data/server/php5.6.0 –with-mysqli –with-pdo-mysql –with-iconv-dir –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir –enable-simplexml –enable-xml –disable-rpath –enable-bcmath –enable-soap –enable-zip –with-curl –enable-fpm –with-fpm-user=www –with-fpm-group=www –enable-mbstring –enable-sockets –with-gd –with-openssl –with-mhash –enable-opcache –disable-fileinfo
make
make install
5,编辑nginx配置文件
cd /data/server/php5.6.0/etc
cp -r /data/server/php5.6.0/etc/php-fpm.conf.default php-fpm.conf
cd /usr/local/src/nginx-1.12.2/conf/ && cp -r nginx-1.12.2 /etc/nginx/nginx.conf
vim nginx.conf
修改以下几点
把第一行注释去掉 ,nobody改为www
表示nginx服务器的权限为www
把45行改为 index index.html index.htm index.php
目的是为了追加index.php让nginx服务器默认支持index.php为首页
把注释去掉,改为
server {
listen 80;
server_name localhost;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root
6,重启nginx服务
/data/server/nginx/sbin/nginx -s stop(关闭nginx)
/data/server/nginx/sbin/nginx (启动nginx)
7,在/data/server/nginx/html下创建一个 pan.html (其中pan可以改为其他)
vim pan.php
8,关闭防火墙和SELINUX
systemctl stop firewalld
setenforce 0
9,打开浏览器,编辑自己IP/pan.php
即会出现下面的页面。