源代码安装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端口的设置
源代码安装Nginx和PHP的更多相关文章
- Building nginx from Sources(从源代码安装nginx)
Building nginx from Sources(从源代码安装nginx) The build is configured using the configure command. 安装用配置 ...
- CentOS 7 源代码安装Nginx
本篇简要介绍CentOS 7 源代码安装Nginx. Preface # yum install epel-release -y # yum group install "Developme ...
- Nginx源代码安装
1.确认系统平台及版本 [root@Trial html]# uname -r 2.6.32-696.6.3.el6.x86_64 [root@Trial html]# cat /etc/redhat ...
- linux 安装 nginx 及反向代理配置
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,以下为Linux centos平台下安装nginx并配置反向代理的过程(采用源码安装的方式) 一:安装 ...
- Linux(Centos)之安装Nginx及注意事项
1.Nginx的简单说明 a. Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,期初开发的目的就是为了代理电子邮件服务器室友:Igor Sysoev开发 ...
- Linux中编译、安装nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器. Nginx 是由Igor Sysoev为俄罗斯访问 ...
- CentOS 6.0最小化编译安装Nginx+MySQL+PHP+Zend
http://www.osyunwei.com/archives/235.html 引言: 操作系统:CentOS 6.0 32位 下载地址:http://mirrors.163.co ...
- Centos 6.5编译安装Nginx+php+Mysql
说明: 操作系统:CentOS 6.5 64位 准备篇: 一.配置好IP.DNS .网关,确保使用远程连接工具能够连接服务器 二.配置防火墙,开启80端口.3306端口 vi /etc/sysconf ...
- centos 6.3 编译安装 nginx +mysql + php
这篇文章是对另一篇文章的整理,作为记录收藏 1,配置防火墙,开启80端口.3306端口 配置iptables,开启80端口.3306端口 vi /etc/sysconfig/iptables -A I ...
- 安装nginx 做反向代理
nginx反向代理配置实例(前nginx+后apache)Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP ...
随机推荐
- vivo 大规模特征存储实践
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/u1LrIBtY6wNVE9lzvKXWjA作者:黄伟锋 本文旨在介绍 vivo 内部的特征存储 ...
- vivo 商城架构升级-SSR 实战篇
一.前言 在前面几篇文章中,相信大家对vivo官网商城的前端架构演变有了一定的了解,从稳步推进前后端分离到小程序多端探索实践,团队不断创新尝试. 在本文中,我们来分享一下vivo官网商城在Node 服 ...
- Spring七种事务传播行为与五种事务隔离级别
一.事务的传播行为:通过Propagation定义: <!-- 配置事务通知 --><tx:advice id="txAdvice" transaction-ma ...
- 入门篇-其之十一-流程控制之break和continue关键字
本文中使用到的工具是Intellij IDEA和JDK 8,需要安装两款工具的请查看这两篇教程:点我查看安装JDK8/11/17教程.点我查看安装Intellij IDEA教程. 一.循环的嵌套 和前 ...
- token原理分析
- 项目管理之问,ChatGPT作答
项目管理 帮我列一份<项目管理>的大纲 当然可以!以下是一个典型的<项目管理>大纲: I. 项目管理概述 A. 项目管理定义和目标 B. 项目管理的重要性和价值 C. 项目管理 ...
- php开发之文件下载的实现
前言 php是网络安全学习里必不可少的一环,简单理解php的开发环节能更好的帮助我们去学习php以及其他语言的web漏洞原理 正文 在正常的开发中,文件下载的功能是必不可少,比如我们在论坛看到好看图片 ...
- python进阶(6)--类
文档目录: 一.创建类 二.使用类与实例 三.继承 ---------------------------------------分割线:正文----------------------------- ...
- 【rt-thread】构建自己的项目工程 -- 初始篇
现以stm32f429igt6芯片的板子 & Keil5编译环境为例,记述构建适配自己板子的rt-thread工程的过程 1.拿到rt-thread源码,进入bsp/stm32/librari ...
- mysql8 initialize 命令 初学版 lower_case_table_names
1. 今天开发找我跟我说 我安装的mysql 不对. 比较蛋疼. 需要修改一个参数 但是数据库已经初始进去了 重装起来比较麻烦. 硬着头皮搞. 2. 参数的名字为: lower_case_tabl ...