欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux(CentOS)环境下安装下载Nginx并配置

程序员文章站 2023-09-28 08:05:57
1、准备工作 选首先安装这几个软件:gcc,pcre(perl compatible regular expression),zlib,openssl。 ngi...

1、准备工作

选首先安装这几个软件:gcc,pcre(perl compatible regular expression),zlib,openssl。

nginx是c写的,需要用gcc编译;nginx的rewrite和http模块会用到pcre;nginx中的gzip用到zlib;

用命令“# gcc”,查看gcc是否安装;如果出现“gcc: no input files”信息,说明已经安装好了。

否则,就需要用命令“# yum install gcc”,进行安装了!一路可能需要多次输入y,进行确认。

安装好后,可以再用命令“#gcc”测试,或者用命令“# gcc -v”查看其版本号。

同样方法,用如下命令安装pcre,zlib,openssl(其中devel,是develop开发包的意思):

# yum install -y pcre pcre-devel 
# yum install -y zlib zlib-devel 
# yum install -y openssl openssl-devel 

2、下载并安装

创建目录(nginx-src)并进去;然后,从官方地址()下载,解压,配置,编译,安装:

# mkdir nginx-src && cd nginx-src 
# wget http://nginxorg/download/nginx-targz 
# tar xzf nginx-targz  
# cd nginx-3 
# /configure 
# make 
# make install 
# whereis nginx 
nginx: /usr/local/nginx 

默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。

# /nginx -h 
nginx version: nginx/3 
usage: nginx [-?hvvtq] [-s signal] [-c filename] [-p prefix] [-g directives] 
options: 
 -?,-h     : this help 
 -v      : show version and exit 
 -v      : show version and configure options then exit 
 -t      : test configuration and exit 
 -q      : suppress non-error messages during configuration testing 
 -s signal   : send signal to a master process: stop, quit, reopen, reload 
 -p prefix   : set prefix path (default: /usr/local/nginx/) 
 -c filename  : set configuration file (default: conf/nginxconf) 
 -g directives : set global directives out of configuration file 

启动:nginx

停止:nginx -s stop

3、添加到系统服务

使用命令“# vi /etc/init.d/nginx”,打开编辑器,输入如下内容:

#!/bin/sh 
# chkconfig: 2345 85 15 
# startup script for the nginx web server 
# description: nginx is a world wide web server  
# it is used to serve html files and cgi 
# processname: nginx 
# pidfile: /usr/local/nginx/logs/nginxpid 
# config: /usr/local/nginx/conf/nginxconf 
 
path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 
desc="nginx deamon" 
name=nginx 
daemon=/usr/local/nginx/sbin/$name 
scriptname=/etc/initd/$name 
 
test -x $daemon || exit 0 
 
d_start(){ 
 $daemon || echo -n "already running" 
} 
 
d_stop(){ 
 $daemon -s quit || echo -n "not running" 
} 
 
 
d_reload(){ 
 $daemon -s reload || echo -n "can not reload" 
} 
 
case "$1" in 
start) 
 echo -n "starting $desc: $name" 
 d_start 
 echo "" 
;; 
stop) 
 echo -n "stopping $desc: $name" 
 d_stop 
 echo "" 
;; 
reload) 
 echo -n "reloading $desc conf" 
 d_reload 
 echo "reload " 
;; 
restart) 
 echo -n "restarting $desc: $name" 
 d_stop 
 sleep 2 
 d_start 
 echo "" 
;; 
*) 
 echo "usage: $scriptname {start|stop|reload|restart}" >&2 
 exit 3 
;; 
esac 
 
exit 0 

保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。

可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

# chmod +x /etc/initd/nginx 
# chkconfig --add nginx 
# chkconfig nginx on/off 
# chkconfig --list nginx 
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。