Centos 7 编译安装 Nginx PHP

Centos 7 编译安装 Nginx PHP

一、安装编译依赖环境

# 使用root用户
yum -y install pcre-devel zlib-devel gcc gcc-c++ make

二、编译安装 Nginx

1、下载源码

mkdir -p /home/src && cd /home/src
wget http://nginx.org/download/nginx-1.23.0.tar.gz
tar -zxvf nginx-1.23.0.tar.gz

2、创建nginx用户组

groupadd -r nginx

3、创建nginx用户

useradd -M -s /sbin/nologin nginx

4、创建安装目录

mkdir -p /home/app/nginx

5、编译安装

# 进入源码目录
cd /home/src/nginx-1.23.0
# 配置编译选项生成Makefile
./configure \
--prefix=/home/app/nginx \
--without-http_memcached_module \
--user=nginx  \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module
# 编译&安装
make && make install

6、检查是否安装成功

# 启动nginx
/home/app/nginx/sbin/nginx
# 检查nginx是否正常
/home/app/nginx/sbin/nginx -t 
# 执行后输出如下信息,如果未提示错误即代表安装成功
nginx: the configuration file /home/app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/app/nginx/conf/nginx.conf test is successful
# 停止nginx
/home/app/nginx/sbin/nginx -s stop

7、编写快捷启动脚本

vi /lib/systemd/system/nginx.service
# 启动脚本代码
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
PIDFile=/home/app/nginx/logs/nginx.pid
ExecStart=/home/app/nginx/sbin/nginx
ExecReload=/home/app/nginx/sbin/nginx -s reload
ExecStop=/home/app/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

8、设置开机启动

systemctl enable nginx.service

9、启动、重启、停止Nginx

# 启动
systemctl start nginx.service
# 重启
systemctl restart nginx.service
# 停止
systemctl stop nginx.service

三、安装 PHP 7

1、下载源码

cd /home/src
wget http://ftp.ntu.edu.tw/php/distributions/php-7.1.30.tar.gz
tar -zxvf php-7.1.30.tar.gz

2、编译安装

# 进入源码目录
cd /home/src/php-7.1.30
# 配置编译选项生成Makefile
./configure \
--prefix=/home/app/php \
--with-config-file-path=/home/app/php/etc \
--enable-fpm \
--with-fpm-user=www  \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache
# 编译&安装
make && make install

3、配置环境变量

# 创建独立环境变量文件
touch /etc/profile.d/php.sh
# 写入变量值
echo 'export PATH=$PATH:/home/app/php/bin/' > /etc/profile.d/php.sh
# 赋以执行权限
chmod 0777 /etc/profile.d/php.sh
# 刷新生效
source /etc/profile.d/php.sh

4、配置php.ini

# 拷贝基础模板
cp php.ini-production /home/app/php/etc/php.ini
# 修改配置
vi /home/app/php/etc/php.ini
# 做以下修改(时区,不显示版本号,开启opcache缓存加速PHP)
# ------------------------------------------------------------
1.找到:;date.timezone =       修改为:date.timezone = PRC
2.找到:expose_php = On        修改为:expose_php = Off
3.找到:opcache.enable=0       修改为:opcache.enable=1
4.在 Dynamic Extensions 代码块中添加 zend_extension=opcache.so
# ------------------------------------------------------------

5、配置php-fpm

# 拷贝基础配置模板
cp /home/app/php/etc/php-fpm.conf.default /home/app/php/etc/php-fpm.conf
cp /home/app/php/etc/php-fpm.d/www.conf.default /home/app/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /home/app/php/bin/php-fpm
# 赋以 php-fpm 执行权限
chmod 0777 /home/app/php/bin/php-fpm
# vi /home/app/php/etc/php-fpm.conf
# 做以下修改
# ----------------------------------------——————————————
1.找到:;daemonize = yes        修改为:daemonize = yes
# ----------------------------------------——————————————
# vim /home/app/php/etc/php-fpm.d/www.conf
# 做以下修改
# ----------------------------------------————————————————————————————
1.找到:listen = 127.0.0.1:9000       修改为:;listen = 127.0.0.1:9000
2.增加:listen = /home/app/php/var/run/php-fpm.sock
3.找到:;listen.owner = www           修改为:listen.owner = www
4.找到:;listen.group = www           修改为:listen.group = www
5.找到:;listen.mode = 0660           修改为:listen.mode = 0660
# ----------------------------------------————————————————————————————

6、测试 php-fpm 是否安装成功

/home/app/php/bin/php-fpm start
# 如提示以下信息即表示安装成功
Starting php-fpm  done

7、编写快捷启动脚本

vi /lib/systemd/system/php-fpm.service
# 启动脚本代码
[Unit]
Description=php-fpm
After=network.target
  
[Service]
Type=forking
PIDFile=/home/app/php/var/run/php-fpm.pid
ExecStart=/home/app/php/bin/php-fpm start
ExecReload=/home/app/php/bin/php-fpm restart
ExecStop=/home/app/php/bin/php-fpm stop
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

8、设置开机启动

systemctl enable php-fpm.service

9、启动、重启、停止 php-fpm

# 启动
systemctl start php-fpm.service
# 重启
systemctl stop php-fpm.service
# 停止
systemctl restart php-fpm.service

四、Nginx PHP 配置

1、修改 /home/app/nginx/conf/nginx.conf

# vim /home/app/nginx/conf/nginx.conf
# 做以下修改
# ----------------------------------------————————————————————————————
1.找到:#user  nobody;       修改为:user  www;
2.在 server 配置节中增加如下内容
location ~ [^/]\.php(/|$) {
    fastcgi_pass     unix:/home/app/php/var/run/php-fpm.sock;
    fastcgi_index    index.php;
    fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include          fastcgi.conf;
}
# ----------------------------------------————————————————————————————

2、重启nginx

systemctl restart nginx

3、测试

#vi /home/app/nginx/html/test.php
# 编写以下内容
#————————————————————————

#————————————————————————
# 用浏览器访问:http://xxx.xxx.xxx.xxx/test.php

你可能感兴趣的:(nginxlinux)