源代码安装Nginx和PHP

一、安装前准备:

有些工具在安装Nginx必备。譬如gcc用来编译C程序,gcc-c++ 用来编译C++程序,wget用来从网络下载文件。

[root@localhost ~]# yum -y install gcc gcc-c++ wget

去Nginx官网下载Nginx包,官网(http://nginx.org/en/download.html)上提供了3中类型的版本:Mainline version(开发版), Stable version(稳定版),Legacy versions(早期版本);感人的是每个版本都有Linux、Windows版本。建议使用稳定版,因为稳定!(哈哈)

[root@localhost ~]# wget http://nginx.org//download/nginx-1.14.2.tar.gz

注意 yum -y install;-y表示在整个过程中全部执行默认的yes

解压缩

[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.14.2.tar.gz
[root@localhost ~]# tar -zxvf nginx-1.14.2.tar.gz

使用cd命令切换到该目录,然后使用ls命令查看该目录下的目录

[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.14.2 nginx-1.14.2.tar.gz
[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# ll
总用量 732
drwxr-xr-x. 6 1001 1001 4096 3月 27 15:54 auto # 存放大量的脚本文件,和configure脚本程序
-rw-r--r--. 1 1001 1001 288742 12月 4 22:52 CHANGES
-rw-r--r--. 1 1001 1001 440121 12月 4 22:52 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 3月 27 15:54 conf # 存放和Nginx配置相关的配置文件
-rwxr-xr-x. 1 1001 1001 2502 12月 4 22:52 configure
drwxr-xr-x. 4 1001 1001 72 3月 27 15:54 contrib
drwxr-xr-x. 2 1001 1001 40 3月 27 15:54 html # 存放默认网站的文件
-rw-r--r--. 1 1001 1001 1397 12月 4 22:52 LICENSE
drwxr-xr-x. 2 1001 1001 21 3月 27 15:54 man # 存放Nginx的帮助文档
-rw-r--r--. 1 1001 1001 49 12月 4 22:52 README
drwxr-xr-x. 9 1001 1001 91 3月 27 15:54 src # 存放Nginx的源代码

二、编译安装Nginx

Nginx的功能是模块化,而模块又依赖一些软件包:pcre-devel 为Nginx提供正则表达式库,zlib-devel为Nginx提供数据压缩的函数库,openssl-devel为Nginx提供密码算法、证书以及ssl协议等功能

[root@localhost nginx-1.14.2]# yum -y install pcre-devel openssl-devel

安装openssl-devel的时候自动会安装zlib-devel

开始编译安装

[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  • --prefix 设置Nginx的安装目录
  • --with-http_ssl_module 选项用于Nginx中允许使用http_ssl_module模块
[root@localhost nginx-1.14.2]# make && make install

没有报错便是晴天

三、Nginx的启动和停止

1.启动

[root@localhost nginx-1.14.2]# find / -name nginx
/root/nginx-1.14.2/objs/nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx

初次启动会报错

httpd (pid 5517) already running

此错误为端口被占用,只有进入root用户,才可以查看所有端口被占用的情况,然后找到进程id,干掉进程。注意在如果是CentOS5、6版本,系统防火墙iptables里添加80端口的对外访问

[root@localhost ~]# netstat -lnp | grep 80
tcp6 0 0 :::80 :::* LISTEN 5517/httpd
unix 2 [ ACC ] STREAM LISTENING 19580 1131/master private/retry
[root@localhost ~]# kill 5517

注意你是CentOS7版本,默认防火墙是firewalld.添加80端口如下:

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]#/usr/local/nginx/sbin/nginx

检查Nginx启动进程

[root@localhost nginx-1.14.2]# ps -aux | grep nginx
root 13563 0.0 0.1 45940 1124 ? Ss 17:30 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 13564 0.0 0.1 46388 1896 ? S 17:30 0:00 nginx: worker process
root 13643 0.0 0.0 112724 988 pts/0 R+ 17:32 0:00 grep --color=auto nginx

前两个是Nginx的主(master)进程和工作(worker)进程,也可以检查端口使用或占用情况

[root@localhost nginx-1.14.2]# netstat -tlnp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17809/nginx: master

2.停止Nginx服务

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost nginx-1.14.2]# ps -aux | grep nginx
root 17401 0.0 0.0 112724 984 pts/0 S+ 18:46 0:00 grep --color=auto nginx

参数-s,表示发信号到主进程,后面跟上stop表示停止服务。这种比较“猛”,无论当前工作进程是否正在处理工作,都会立即停止。还有一种停止,当进程完成当前任务再停止

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -s quit

当然还可以用kill或killall命令杀死进程

[root@localhost nginx-1.14.2]# kill Nginx 主进程Id
[root@localhost nginx-1.14.2]# killall Nginx

3.平滑重启

在Nginx已经启动运行的情况下,重新加载配置文件

[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -s reload

四、访问Nginx

在服务器内使用curl请求访问

[root@localhost nginx-1.14.2]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx-1.14.2]#

看到Welcome to nginx!就证明Nginx安装ok!,但是我们利用浏览器访问,不一定OK。CentOS7的默认防火墙为firewallD。而防火墙默认并不一定就开发了80端口。解决办法要么关闭防火墙

[root@localhost conf]# systemctl stop firewalld

或者在防火墙里永久开启3690端口

[root@localhost conf]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@localhost conf]# firewall-cmd --reload
success
[root@localhost nginx-1.14.2]# firewall-cmd --zone=public --list-ports
80/tcp
[root@localhost nginx-1.14.2]#

五、安装PHP

先到PHP官网下载需要安装的php版本,这里我们安装最新的稳定版本php7.3.3。. 下载tar包,解压

[root@localhost lnmp]# wget http://cn2.php.net/distributions/php-7.3.3.tar.gz
[root@localhost lnmp]# tar -zxvf php-7.3.3.tar.gz
[root@localhost lnmp] cd php-7.3.3

在解压的目录里,PHP提供了configure文件用户编译安装,你可以使用./configure --help查看详细的编译配置参数,选项太多,我们就捡一些常用的选项。

选项 说明
--prefix PHP的安装目录,一般我们设为/usr/local/php
--enable-fpm 开启PHP的FPM功能,提供PHP FastCGI管理器
--with-zlib 包含zlib库,支持数据压缩和解压缩
--enable-zip 开启ZIP功能
--enable-mbstring 开启mbstring功能,用于多字节字符串处理
--with-mcrypt 包含mcrypt加密支持(需要依赖libmcrypt)
--with-mysql 包含MySQL数据库访问支持
--with-mysqli 包含增强版的MySQL数据库访问支持
--with-pdo-mysql 包含基于PDO的数据库访问
--with-gd 包含GD库支持,用于PHP图像处理
--with-jpeg-dir 包含jpeg图像格式处理库(依赖libjpeg-devel)
--with-png-dir 包含png图像格式处理库(依赖libjpng-devel)
--with-freetype-dir 包含FreeType字体图像格式处理库(依赖freetype-devel)
--with-curl 包含curl支持(依赖curl-devel)
--with-opensll 包含OpenSSL支持(依赖openssl-devel)
--with-mhash 包含mhash支持加密支持
--enable-bcmath 开启精准计算功能
--enable-opcache 开启opcache功能,一种PHP代码的优化器

注意:enable用于开启PHP内置的功能,而with依赖于系统中的共享库,如果系统中没有则需要安装依赖包

安装依赖

[root@localhost php-7.3.3] yum install -y libxml2 libxml2-devel openssl-devel \
curl-devel libjpeg-devel libpng-devel freetype-devel libzip-devel

配置检测

[root@localhost php-7.3.3]  ./configure --enable-fpm --prefix=/usr/local/php --with-zlib --enable-zip --enable-mbstring --with-mysqli --with-pdo-mysql --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-curl --with-openssl --with-mhash --enable-bcmath --enable-opcache

如果在configure过程中出现如下报错:

checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

原因是libzip版本的问题 我们手动安装解决

[root@localhost php-7.3.3] cd ..
#先删除旧版本
[root@localhost lnmp] yum remove -y libzip
#下载编译安装
[root@localhost lnmp] wget https://nih.at/libzip/libzip-1.2.0.tar.gz
[root@localhost lnmp] tar -zxvf libzip-1.2.0.tar.gz
[root@localhost lnmp] cd libzip-1.2.0
[root@localhost libzip-1.2.0] ./configure
[root@localhost libzip-1.2.0] make && make install
[root@localhost libzip-1.2.0] cd ../php-7.3.3

再进行一次编译前配置检测,然后编译安装.(老手先不着急敲回车,把下面的报错解决看完)

cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
[root@localhost php-7.3.3] make && make install

在编译过错中回报这样一个错误:

compilation terminated.
make: *** [ext/zip/php_zip.lo] Error 1

找不到文件,加zipconf.h软连接,解决方法:

[root@localhost php-7.3.3] cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

PHP简单的测试使用: 在家目录 创建一个PHP脚本

[root@localhost ~] echo '<?php echo 8+17; ?>' >> demo.php
[root@localhost ~] /usr/local/php/bin/php demo.php

六、Nginx整合PHP

首先:配置php.ini文件,由于在配置时我们并没有指定php.ini的加载位置,默认在安装php安装目录的lib目录下,所以我 们移动配置文件到 /usr/local/php/lib 目录下

[root@localhost ~] cp /root/lnmp/php-7.3.3/php.ini-development /usr/local/php/lib/php.ini

/usr/local/php/etc/php-fpm.conf 最后一行可以看到 include=/usr/local/php/etc/php-fpm.d/*.conf,所以需要执行以下步骤

[root@localhost ~] cd /usr/local/php/etc/php-fpm.d
[root@localhost php-fpm.d]cp www.conf.default www.conf
[root@localhost php-fpm.d]cd ~

生成配置文件启动php-fpm

[root@localhost ~] cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost ~] /usr/local/php/sbin/php-fpm

修改nginx配置以支持php应用,找到Nginx的主配置文件 /usr/local/nginx/conf/nginx.conf

location / {
root html;
index index.php index.html index.htm;
}

下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}

或是

location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}

重启nginx使配置生效

[root@localhost php-fpm.d] /usr/local/nginx/sbin/nginx -s stop
[root@localhost php-fpm.d] /usr/local/nginx/sbin/nginx

创建测试文件

echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/info.php

内部测试

[root@localhost php-fpm.d] curl localhost/info.php

外部访问的时候,注意防火墙对80端口的设置

源代码安装Nginx和PHP的更多相关文章

  1. Building nginx from Sources(从源代码安装nginx)

    Building nginx from Sources(从源代码安装nginx) The build is configured using the configure command.  安装用配置 ...

  2. CentOS 7 源代码安装Nginx

    本篇简要介绍CentOS 7 源代码安装Nginx. Preface # yum install epel-release -y # yum group install "Developme ...

  3. Nginx源代码安装

    1.确认系统平台及版本 [root@Trial html]# uname -r 2.6.32-696.6.3.el6.x86_64 [root@Trial html]# cat /etc/redhat ...

  4. linux 安装 nginx 及反向代理配置

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,以下为Linux centos平台下安装nginx并配置反向代理的过程(采用源码安装的方式) 一:安装 ...

  5. Linux(Centos)之安装Nginx及注意事项

    1.Nginx的简单说明 a.  Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,期初开发的目的就是为了代理电子邮件服务器室友:Igor Sysoev开发 ...

  6. Linux中编译、安装nginx

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器. Nginx 是由Igor Sysoev为俄罗斯访问 ...

  7. CentOS 6.0最小化编译安装Nginx+MySQL+PHP+Zend

    http://www.osyunwei.com/archives/235.html 引言: 操作系统:CentOS 6.0 32位         下载地址:http://mirrors.163.co ...

  8. Centos 6.5编译安装Nginx+php+Mysql

    说明: 操作系统:CentOS 6.5 64位 准备篇: 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接服务器 二.配置防火墙,开启80端口.3306端口 vi /etc/sysconf ...

  9. centos 6.3 编译安装 nginx +mysql + php

    这篇文章是对另一篇文章的整理,作为记录收藏 1,配置防火墙,开启80端口.3306端口 配置iptables,开启80端口.3306端口 vi /etc/sysconfig/iptables -A I ...

  10. 安装nginx 做反向代理

    nginx反向代理配置实例(前nginx+后apache)Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP ...

随机推荐

  1. 领域驱动设计(DDD)实践之路(二):事件驱动与CQRS

    本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/Z3uJhxJGDif3qN5OlE_woA作者:wenbo zhang [领域驱动设计实践之 ...

  2. 传统能源转型:数字孪生智慧火电厂 3D 可视化

    前言 火力发电厂满足了全世界将近五成的电力需求,在我国超过半数以上的电力来源于火力发电.随着双碳政策的推行,在国家清洁能源消纳和环保的需求下,对火电厂在深度调峰.超低排放.灵活运行等方面提出了更高要求 ...

  3. 4、SpringBoot连接数据库引入druid

    系列导航 springBoot项目打jar包 1.springboot工程新建(单模块) 2.springboot创建多模块工程 3.springboot连接数据库 4.SpringBoot连接数据库 ...

  4. Vue+Element前端导入导出Excel

    1 <el-upload 2 class="upload-demo" 3 :action="uploadUrl()" 4 :limit="1&q ...

  5. windows10/liunx创建空大文件

    1.windows10创建空大文件打开cmd命令,进入需要创建文件的目录,使用以下命令创建 fsutil file createnew test001.txt 1073741824 最后的数字代表文件 ...

  6. 2024-01-20:用go语言,小扣在探索丛林的过程中,无意间发现了传说中“落寞的黄金之都“, 而在这片建筑废墟的地带中,小扣使用探测仪监测到了存在某种带有「祝福」效果的力场, 经过不断的勘测记录,

    2024-01-20:用go语言,小扣在探索丛林的过程中,无意间发现了传说中"落寞的黄金之都", 而在这片建筑废墟的地带中,小扣使用探测仪监测到了存在某种带有「祝福」效果的力场, ...

  7. go 变量逃逸分析

    0. 前言 在 小白学标准库之 reflect 篇中介绍了反射的三大法则以及变量的逃逸分析.对于逃逸分析的介绍不多,大部分都是引自 Go 逃逸分析.不过后来看反射源码的过程中发现有一种情况 Go 逃逸 ...

  8. 设备共享分配:虚拟化和 SRIOV

    SRIOV 简介 OpenStack 自 Juno 版本开始引入 SRIOV,SRIOV(Single Root I/O Virtualization) 是将 PCIe(PCI) 设备虚拟化成虚拟 P ...

  9. python中BeautifulSoup库使用小结

    转载请注明出处: BeautifulSoup是一个用于解析HTML和XML文档的Python库,它提供了一些简单但强大的API,让你可以从文档中提取数据.以下是一些BeautifulSoup的主要特性 ...

  10. 分享一个在线二维码生成器(基于qrcode.js开发)

    一种二维码扫描与生成的工具, 它可生成个性化二维码, 支持文本.网址.图片.短信.电话等格式及主题,提供融合码功能 演示地址 https://qrcode.gitapp.cn 关键代码 var qrc ...