帮助与文档

当前位置: 主页 > 系统相关 > 正文

Centos7如何安装nginx

时间:2024/6/2 11:18:28   来源:米安网络   作者:云主机配置专家   点击:266次

Centos如何安装nginx?本教程以centos7系统为例

本配置适用于centos7版本


1.安装vim编辑工具


yum install vim -y

2.安装lrzsz上传工具

yum install lrzsz -y


安装完后可以使用rz命令上传所需要的软件工具(最好进入家目录/home在上传方便以后查找)


3.指定环境版本和上传环境安装包

nginx-1.12.2.tar具体版本不一定,这边只是随机安装1个版本,具体看需要什么版本

4.安装关联包

yum -y install pcre-devel

yum -y install openssl openssl-devel

yum -y install gcc-c

5.安装nginx-1.12.2.tar

6.进入上传文件目录解压nginx-1.12.2.tar文件

[root@vt178m5c home]# tar -zxvf nginx-1.12.2.tar.gz

7.进入nginx-1.12.2解压文件开始编译安装nginx

[root@vt178m5c home]# cd nginx-1.12.2

[root@vt178m5c nginx-1.12.2]# ./configure --prefix=/usr/local/nginx \

> --with-http_stub_status_module \

> --with-http_gzip_static_module \

> --with-http_flv_module \

> --with-http_ssl_module \

> --http-client-body-temp-path=/usr/local/nginx/client_body_temp \

> --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \

> --http-proxy-temp-path=/usr/local/nginx/proxy_temp \

> --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \

> --http-scgi-temp-path=/usr/local//nginx/scgi_temp

8.编译完确认没有报错后开始安装nginx

[root@vt178m5c nginx-1.12.2]# make && make install

9.修改nginx配置文件

[root@vt178m5c nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

找到

#error_log logs/error.log;
#access_log logs/access.log main;

取消注释与修改

error_log logs/error.log;
access_log logs/access.log ;

找到

index index.html index.htm;

修改为

index index.php index.html index.htm;

找到


取消注释, /scripts$fastcgi_script_name;修改为$document_root$fastcgi_script_name;


这个配置的意思是 在浏览器中访问的.php文件,实际读取的是 $document_root(网站根目录)下的.php文件 -- 也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不会去网站根目录下访问.php文件,所以返回空白

配置项目中:include fastcgi_params; fastcgi_params 文件中含有各个nginx常量的定义,默认情况 SCRIPT_FILENAME = $fastcgi_script_name

10.编写nginx启动脚本

[root@vt178m5c nginx-1.12.2]# vim /etc/init.d/nginx

脚本代码:

#! /bin/bash
#chkconfig: 2345 80 90
#description:nginx run
# nginx启动脚本
# @author liut
# @version 0.0.1
# @date 2018-2-9
PATH=/usr/local/nginx/conf
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/nginx
CONFIGFILE=$PATH/$NAME.conf
PIDFILE=$PATH/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start()
{
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop()
{
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload()
{
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

11.编写完脚本记得修改下脚本权限

12.nginx启动|停止|重启

[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx start 启动

[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx stop 停止

[root@vt178m5c nginx-1.12.2]# /etc/init.d/nginx restart 重启

版权保护: 本文「Centos7如何安装nginx」由 云主机配置专家 原创,转载请保留链接: https://www.miandns.com/docs/system/1137.html

联系客服

0312-5535754

微信公众号