CentOS 7 安装Nginx
互联网的开放性成就了程序员的未来,I Love Internet!
一、安装依赖包
顺序安装依赖包:遇到【y/n】都选y回车
yum install gcc*
yum install pcre*
yum install zlib*
yum install openssl*
yum install links*
yum install lsb
查看系统版本:lsb_release -a
二、下载安装包
进入opt目录,创建software文件夹 存放Nginx下载的安装程序
mkdir /opt/software
cd /opt/software/
wget http://Nginx.org/download/Nginx-1.10.2.tar.gz
tar -zxvf Nginx-1.10.2.tar.gz
cd Nginx-1.10.2/
mkdir -p /var/cache/Nginx
wget https://www.openssl.org/source/openssl-1.0.2j.tar.gz
tar -zxvf openssl-1.0.2j.tar.gz
四、编译 安装
cd /opt/software/Nginx-1.10.2/
./configure \
--prefix=/usr/local/Nginx\ --sbin-path=/usr/sbin/Nginx \ --conf-path=/etc/Nginx/Nginx.conf \ --error-log-path=/var/log/Nginx/error.log \ --http-log-path=/var/log/Nginx/access.log \ --pid-path=/var/run/Nginx.pid \ --lock-path=/var/run/Nginx.lock \ --http-client-body-temp-path=/var/cache/Nginx/client_temp \ --http-proxy-temp-path=/var/cache/Nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/Nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/Nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/Nginx/scgi_temp \ --user=nobody \ --group=nobody \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-ipv6 \ --with-http_v2_module \ --with-threads \ --with-stream \ --with-stream_ssl_module \ --with-openssl=/opt/software/Nginx-1.10.2/openssl-1.0.2j
make & make install
五、启动验证安装成功
/usr/local/Nginx/sbin/Nginx
links 127.0.0.1
六、开放80访问端口
开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
关闭防火墙
service firewalld stop
七、编辑启动脚本
vim /etc/init.d/Nginx
复制一下代码到Nginx文件中
--------------------开始-----------------
@H_652_301@#!/bin/sh
# chkconfig: 2345 80 20
# Description: Start and Stop Nginx
# Provides: Nginx
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=Nginx
Nginx_BIN=/usr/local/Nginx/sbin/$NAME
CONFIGFILE=/usr/local/Nginx/conf/$NAME.conf
PIDFILE=/usr/local/Nginx/logs/pid/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
echo -n "Starting $NAME... "
if netstat -tnpl | grep -q Nginx;then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
$Nginx_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " Failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if ! netstat -tnpl | grep -q Nginx; then
echo "$NAME is not running."
exit 1
fi
$Nginx_BIN -s stop
if [ "$?" != 0 ] ; then
echo " Failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if netstat -tnpl | grep -q Nginx; then
PID=`pidof Nginx`
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit 0
fi
;;
force-quit)
echo -n "Terminating $NAME... "
if ! netstat -tnpl | grep -q Nginx; then
echo "$NAME is not running."
exit 1
fi
kill `pidof $NAME`
if [ "$?" != 0 ] ; then
echo " Failed"
exit 1
else
echo " done"
fi
;;
restart)
$SCRIPTNAME stop
sleep 1
$SCRIPTNAME start
;;
reload)
echo -n "Reload service $NAME... "
if netstat -tnpl | grep -q Nginx; then
$Nginx_BIN -s reload
echo " done"
else
echo "$NAME is not running,can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$Nginx_BIN -t
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"
exit 1
;;
esac
-----------------结束---------------------
:wq! 保存
分配可执行权限
chmod a+x /etc/init.d/Nginx
重启服务器:
reboot
启动
service Nginx start
停止
service Nginx stop
重启
service Nginx reconfigure
查看状态
service Nginx status
八、配置开机启动Nginx
新增Nginx.service文件
vim /lib/systemd/system/Nginx.service
------------------内容--------------------
[Unit]
Description=Nginx
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/Nginx start
ExecReload=/etc/init.d/Nginx restart
ExecStop=/etc/init.d/Nginx stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
------------------结束--------------------
保存:wq!
启用:
systemctl enable Nginx.service
测试配置是否成功:
systemctl start Nginx.service
查看是否启动:
netstat -lntp|grep Nginx
九、配置Nginx.conf
重点内容根据一下内容修改Nginx配置项,没有无需改动,相同无需改动.
vim /usr/local/Nginx/conf/Nginx.conf
-------------------开始---------------------
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server_tokens off;
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
}
}
-------------------结束---------------------
十、部署Tomcat集群
vim /usr/local/Nginx/conf/Nginx.conf
-------------------开始---------------------
未完待续
-------------------结束---------------------