安装需要准备过程
1.安装make
yum -y install gcc automake autoconf libtool make
2.安装gcc c+
yum install gcc gcc-c++
3.安装pcre
yum install -y pcre pcre-devel
4.安装zlib
yum install -y zlib zlib-devel
5.安装openssl
yum install -y openssl openssl-devel
注意:上述安装pcre、zlib、openssl我是直接使用yum进行的安装。无法指定版本,也可以使用wget进行下载【需要安装wget.安装命令:yum -y install wget】。下载指定的版本。
我这里下载到了/home/app中。大家可以自己指定存储位置。
安装pcre
cd /home/app
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -zxvf pcre-8.43.tar.gz
cd pcre-8.43/
./configure
make && make install
安装zlib
cd /hoem/app
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make && make install
安装openssl
cd /hoem/app
wget http://www.openssl.org/source/openssl-1.1.1c.tar.gz
tar -zxvf openssl-1.1.1c.tar.gz
cd openssl-1.1.1c/
./config
make && make install
开始安装Nginx
一.下载nginx包,并解压【我这里下载的是1.18.0,版本可以根据各自的需要下载。下载到/home/app中,大家下载位置随意】
cd /home/app
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
二.配置安装【注意:这一步需要在解压后的安装包中进行】
注:这里 的配置是我自己定义的,各位可自己选择配置。也可以不加载配置直接使用 ./configure也可以
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi
configure命令支持的参数详解的网址【可以用谷歌浏览器翻译后查看,很详细】
http://nginx.org/en/docs/configure.html
三.安装命令
make && make install
四.启动nginx
mkdir -p /var/tmp/nginx/client
/usr/local/nginx/sbin/nginx
五.设置软连接,使nginx命令可以使用
ln -sf /usr/local/nginx/sbin/nginx /usr/sbin
软连接建立后使用 nginx -v 可查看nginx的版本
使用 nginx -t 会出现以下结果,代表安装成功。
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
六.开启端口
centos7的防火墙firewall默认关闭http请求及80端口,所以需先打开防火墙
首先查看防火墙状态。使用:firewall-cmd --state。 返回running表示防火墙在运行。
然后依次使用以下四条命令
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
使用netstat -ntlp查看,80端口已开。
如果使用netstat -ntlp报错:-bash: netstat: 未找到命令. 使用:yum -y install net-tools 安装后才使用上述命令
在浏览器里输入你服务器的地址,出现以下页面。nginx安装成功。
七.将nginx设置为系统service
原因:
因为我是源码安装,而源码编译的一个缺陷是没法将安装好的应用设置为系统的service, 即无法使用 service 服务名 start | stop | restart
等命令统一操作。
第一步:编写/etc/init.d/nginx
文件
vim /etc/init.d/nginx
然后在文件中添加以下内容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
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
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
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
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
第二步:给该文件添加可执行权限:
chmod a+x /etc/init.d/nginx
第三步:将一个新服务添加到启动列表中【注:同样是添加开机自启操作】
chkconfig --add /etc/init.d/nginx
第四步:配置启动
chkconfig nginx on
配置成功后首先使用
ps aux | grep nginx 命令查看nginx是否在运行。
使用kill命令将nginx杀死。
然后就可以使用service命令了。
注:我是这样操作的,一开始没用kill提示报错,我kill以后就可以了。我看着其他的没有这种情况,配置成功后直接使用service命令就可以了
service操作nginx命令
service nginx start
service nginx stop
service nginx configtest
service nginx reload
八.配置开机自启
使用 vim /etc/rc.d/rc.local 命令在文件最后添加
/usr/local/nginx/sbin/nginx
注:这里是我nginx的安装启动的路径,要改为自己的。