JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

小水谈Nginx---Nginx安装

wys521 2024-09-06 04:28:08 精选教程 28 ℃ 0 评论

介绍:

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。

YUM安装方法:

安装epel源

yum install epel-release -y

安装nginx

yum install nginx -y

启动

service nginx start

手动安装方法:

1.安装所需的库

yum -y install gcc gcc-c++ openssl openssl-devel 

2.建立相关目录以及用户组

#安装包存放目录:/main/soft
#服务安装目录:/main/server
mkdir -p /main/soft
mkdir /main/server
/usr/sbin/groupadd www
/usr/sbin/useradd -s /sbin/nologin -M -g www www
mkdir -p /main/web/www
chown -R www.www /main/web/www
chmod g+s /main/web/www
mkdir -p /main/web/logs
mkdir -p /main/web/temp
mkdir -p /main/web/proxy_temp_dir
mkdir -p /main/web/proxy_cache_dir
chown -R www:www /main/web/*

3.下载安装包(可根据需求在官网下载最新版本)

nginx(http://image.wohenniucha.com/upload/2018/05/nginx-1.8.0.tar.gz)

pcre(http://image.wohenniucha.com/upload/2018/05/pcre-8.38.tar.gz)

4.nginx安装

4.1安装Nginx所需的pcre库

#解压并安装 
cd /main/soft
tar zxvf pcre-8.38.tar.gz
cd pcre-8.38/
./configure
make && make install

4.2安装nginx

#解压并安装 
cd /main/soft
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --user=www --group=www --prefix=/main/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-pcre=../pcre-8.38 --with-pcre-jit
make && make install 

4.3修改nginx.conf配置文件

vi /main/server/nginx/conf/nginx.conf
#修改前面几行为:
user www www;
#worker_processes:CPU核心数*2
worker_processes 4;
error_log /main/web/logs/nginx_error.log crit;
pid logs/nginx.pid;
events{
 use epoll;
 worker_connections 65535;
}

4.4测试

#测试配置文件
/main/server/nginx/sbin/nginx -t 
#如果显示下面信息,即表示配置没问题 
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful 

4.5启动nginx服务

/main/server/nginx/sbin/nginx 
#查看nginx进程,显以类似下面的信息,即表示nginx已经启动
ps aux|grep nginx 
root 22900 0.0 0.1 43216 1576 ? Ss 08:23 0:00 nginx: master process /usr/local/server/nginx/sbin/nginx
 www 23019 0.0 2.6 68816 27160 ? S 08:48 0:00 nginx: worker process 
 www 23020 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 
 www 23021 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 
 www 23022 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 

4.6编写nginx启动服务

vi /etc/init.d/nginx 
##输入以下代码并保存
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /main/server/nginx/conf/nginx.conf
# pidfile: /main/server/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/main/server/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/main/server/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
 [ -x $nginx ] || exit 5
 [ -f $NGINX_CONF_FILE ] || exit 6
 echo -n $"Starting $prog: "
 daemon $nginx -c $NGINX_CONF_FILE
 retval=$?
 echo
 [ $retval -eq 0 ] && touch $lockfile
 return $retval
}
stop() {
 echo -n $"Stopping $prog: "
 killproc $prog -QUIT
 retval=$?
 echo
 [ $retval -eq 0 ] && rm -f $lockfile
 return $retval
}
restart() {
 configtest || return $?
 stop
 start
}
reload() {
 configtest || return $?
 echo -n $"Reloading $prog: "
 killproc $nginx -HUP
 RETVAL=$?
 echo
}
configtest() {
 $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
 status $prog
}
rh_status_q() {
 rh_status >/dev/null 2>&1
}
case "$1" in
 start)
 rh_status_q && exit 0
 $1
 ;;
 stop)
 rh_status_q || exit 0
 $1
 ;;
 restart|configtest)
 $1
 ;;
 reload)
 rh_status_q || exit 7
 $1
 ;;
 status)
 rh_status
 ;;
 *)
 echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"
 exit 2
esac

4.7设置权限并添加到启动服务列表中

chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig --level 345 nginx on

5.防火墙设置

#端口开放 
iptables -I INPUT -p tcp --dport 80 -j ACCEPT 
/etc/rc.d/init.d/iptables save 

6.测试

http://IP

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表