您的位置:首页 > 运维架构 > Nginx

Centos 7 Nginx的安装和配置

2018-01-15 11:26 381 查看
进入Nginx官网站 http://nginx.org/,进入后选择最新的版本,进入页面后在
Stable version
中选择
nginx-*
右键复制链接,然后用
wget
下载

[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget http://nginx.org/download/nginx-1.12.0.tar.gz[/code] 
如果提示wget命令未找到,则执行

[root@iZuf6b4wamau0tcvk7hwglZ ~]# sudo yum install wget


下载完用 ll 命令查看刚刚下载获得的
nginx-*.tar.gz
文件,用下面命令解压它

[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf nginx-1.12.0.tar.gz


-z: 表示使用gzip的属性。

-x: 解开一个压缩文件的参数指令。

-v: 表示压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!

-f: 使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!

参考资料

安装Nginx前,需要安装以下三个依赖包:

gzip 模块需要
zlib


rewrite 模块需要
pcre


ssl 功能需要
openssl


https://www.openssl.org/source/

[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget https://www.openssl.org/source/openssl-fips-2.0.14.tar.gz[/code] 
http://www.zlib.net/

[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget http://www.zlib.net/zlib-1.2.11.tar.gz[/code] 
(http://www.pcre.org/)[http://www.pcre.org/

[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz[/code] 
下载完成后安装,依赖包安装顺序依次为:
openssl
zlib
pcre
, 然后安装
Nginx


1.安装openssl

[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf openssl-fips-2.0.14.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd openssl-fips-2.0.14/
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# ./config
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# make
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# make install


2.安装zlib

[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf zlib-1.2.11.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd zlib-1.2.11/
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# ./configure
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# make
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# make install


安装gcc g++

yum install gcc-c++


3.安装pcre

[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf pcre-8.39.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd pcre-8.39/
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# ./configure
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# make
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# make install


4.安装nginx

[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf nginx-1.12.0.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd nginx-1.12.0/
[root@iZ2844brz0xZ nginx-1.12.0]# ./configure --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-fips-2.0.14
[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# make
[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# make install


检测是否安装成功

[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# cd /usr/local/nginx/sbin/
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -t


若出现下方信息表示安装成功

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


最后一步:启动Nginx

[root@iZuf6b4wamau0tcvk7hwglZ sbin]#  ./nginx


查看端口

[root@iZuf6b4wamau0tcvk7hwglZ sbin]# netstat -ntlp


出现以下结果



至此,nginx的安装完成‘

配置Nginx

进入Nginx目录,编辑
nginx.conf
配置文件

[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd /usr/local/nginx/
[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd conf/
[root@iZuf6b4wamau0tcvk7hwglZ conf]# vim nginx.conf


看到
server
模块下有:

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


这段表示所有的文件都会进入html这个目录下,我们现在编写与php相关的语句

在下面我们可以找到配置文件已经帮我们写好了我们只需要把前面的注释符号
#
去掉就好了

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
include        fastcgi_params;
}


输入
:wq
保存退出

进入nginx的
html
目录,新建一个文件测试一下

[root@iZuf6b4wamau0tcvk7hwglZ conf]# cd ..
[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd html/
[root@iZuf6b4wamau0tcvk7hwglZ html]# vim test.php


输入

<?php
phpinfo();
?>


忘了说,修改完配置文件记得要重新启动nginx哦

[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd /usr/local/nginx/sbin/
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -s reload


在花费超多时间和精力再查阅各种博客的配置LNMP后,终于配好了。其实以前配过一次,只不过那时候配的是简陋版,并且自己也对其中过程不是很熟悉。之前配好后没有进行记录,很多大佬都有将他们的配置过程写在博客中,于是这次重新配置,并记录完毕。

说说其中的坑吧,在最后的创建test.php后,在浏览器上显示file not found,本来想询问阿里客服,了解是fpm没配好还是哪个路径没设好,当时毫无头绪。结果阿里客服推荐我去那边收费的区域合作机构帮忙配置,又只好硬着头皮再去翻翻繁多的资料,没想到最后居然配好了。(此刻刚配好的我在写这篇文章时就已经笑出了声)果然是/usr/local/nginx/conf 下的 nginx.conf 里的路径有问题。

这是之前的

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;
include        fastcgi_params;
}


改为

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include        fastcgi_params;
}


忘了说,修改完配置文件记得要重新启动nginx哦

[root@iZuf6b4wamau0tcvk7hwglZ~]# cd /usr/local/nginx/sbin/
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -s reload


打开网址,运行成功~

参考资料

http://www.cnblogs.com/hzh19870110/p/6100661.html 作者:grhlove123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: