源代码安装Nginx和PHP

源代码安装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端口的设置





posted @   成文的博客  阅读(186)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示