nginx整合php搭建网站

摘要:nginx整合php搭建网站

Linux平台

安装nginx

  1. 下载nginx、pcre、zlib源码包并解压缩,官网地址:
    nginx: http://nginx.org/
    pcre: http://www.pcre.org/
    zlib: http://www.zlib.net/
    1
    2
    3
    4
    5
    6
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
    wget http://www.zlib.net/zlib-1.2.11.tar.gz
    tar -zxvf nginx-1.12.2.tar.gz
    tar -zxvf pcre-8.41.tar.gz
    tar -zxvf zlib-1.2.11.tar.gz
  2. 配置、编译、安装。
    1
    2
    3
    cd nginx-1.12.2/
    ./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11
    make && make install
  3. 启动nginx
    1
    /usr/local/nginx/sbin/nginx
  4. 访问http://localhost/ 验证服务启动成功。(如果是远程访问,注意防火墙配置。)

安装php

  1. 通过yum源安装php及php-fpm
    1
    2
    yum install -y php
    yum install -y php-fpm
  2. 启动php-fpm
    1
    service php-fpm start
  3. 查看是否启动成功
    1
    ps -ef | grep php-fpm

nginx整合php

  1. 编辑nginx配置文件nginx.conf,修改server节点相关配置
1
vim /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#...
server {
listen 80;
server_name abc.com www.abc.com;

location / {
root /opt/mysite/www.abc.com; #网站文件根目录
index index.php; #默认首页
}

location ~ \.php$ {
root /opt/mysite/www.abc.com; #网站文件根目录
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#...
}
#...
  1. 重启nginx
    1
    /usr/local/nginx/sbin/nginx -s reload