centos6.5服务器安装Nginx设置服务和开机自启的方法
本文介绍了centos6.5服务器安装Nginx设置服务和开机自启的方法,分享给大家,也给自己留个笔记
1、安装Nginx及其依赖
首先是老套路,使用ssh链接服务器,还记得以前的代码吗?
ssh-t用户名@服务器IP或者域名-p22 ssh-troot@acheng1314.cn-p22
在终端中输入上面命令按下回车,要求我们输入密码,这个密码是不可见的,所以一定要输入正确。
链接到服务器后,我们切换到常用的安装路径,当然我服务器上面的安装路径是/usr/src,接着开始在终端操作:
cd/usr/src mkdirNginx yum-yinstallzlibzlib-developensslopenssl--develpcrepcre-devel wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz tar-zxvfpcre-8.40.tar.gz cdpcre-8.40 ./configure make makeinstall cd.. wgethttp://zlib.net/zlib-1.2.11.tar.gz tar-zxvfzlib-1.2.11.tar.gz cdzlib-1.2.11 ./configure make makeinstall cd.. wgethttp://www.openssl.org/source/openssl-fips-2.0.14.tar.gz tar-zxvfopenssl-fips-2.0.14.tar.gz yum-yinstallopensslopenssl-devel wgethttp://nginx.org/download/nginx-1.4.2.tar.gz tar-zxvfnginx-1.4.2.tar.gz cdnginx-1.4.2 ./configure--prefix=/opt/nginx--with-http_stub_status_module--with-http_ssl_module--with-pcre make makeinstall
到这里来讲,我们的nginx安装完成了,但是我们还需要做更多的事情,那就是配置服务器,添加ssl访问,设置服务和开机启动
2、配置服务器
互联网上关于服务器设置的很多,但是准确阐述的却不是那么多,而我刚好是在看了他们的东西后就呵呵了。正确的配置方法如下:
cd/opt/nginx/conf vinginx.conf
我的nginx.conf如下:
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
#'$status$body_bytes_sent"$http_referer"'
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
#注意这里是设置本机的相关的东西,建议不要更改
server{
listen80;
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
roothtml;
indexindex.htmlindex.htm;
#proxy_passhttp://localhost;
#proxy_set_headerHost$host;
#proxy_set_headerX-Real-IP$remote_addr;
#proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#proxythePHPscriptstoApachelisteningon127.0.0.1:80
#
#location~\.php${
#proxy_passhttp://127.0.0.1;
#}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
#location~\.php${
#roothtml;
#fastcgi_pass127.0.0.1:9000;
#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#concurswithnginx'sone
#
#location~/\.ht{
#denyall;
#}
}
#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/{
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#这里是设置本机的https访问的,这里必须设置才能正确时https
#HTTPSserver
#
server{
listen443;
server_namelocalhostacheng1314.cnwww.acheng1314.cn;
sslon;
#这里是你申请的签名,扔到conf下面的cert目录中
ssl_certificatecert/214217283570796.pem;
ssl_certificate_keycert/214217283570796.key;
ssl_session_timeout5m;
ssl_protocolsTLSv1TLSv1.1TLSv1.2;
ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_cipherson;
location/{
#roothtml;
#indexindex.htmlindex.htm;
proxy_passhttp://localhost;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
}
#这里是设置域名跳转的,转发这些域名到本机的8080端口,
server{
listen80;
server_name*.acheng1314.cnacheng1314.cn;
location/{
proxy_passhttp://localhost:8080/;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
}
}
}
其实在写这个的时候必须注意的是,不管什么应用程序端口不能冲突!比如说我的nginx是绑定的80端口,如果tomcat再设定80端口,那么我的设置就算是绑定到localhost去也是会转发失败的!毕竟网络端口只能一个应用程序占用。
/opt/nginx/sbin/nginx-t /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx-t /sbin/iptables-IINPUT-ptcp--dport443-jACCEPT /etc/rc.d/init.d/iptablessave /etc/init.d/iptablesstatus
走到这一步,我们可以测试一下服务器了,按照正常的来讲,我现在的服务器已经是http和https都已经完全支持了。
3、设置服务和自启
其实说来,这里基本也没啥注意的,只要nginx路径设置正确即可。
#!/bin/sh
#Name:nginx4comex
#nginx-thisscriptstartsandstopsthenginxdaemon
#
#description:NginxisanHTTP(S)server,HTTP(S)reverse\
#proxyandIMAP/POP3proxyserver
#processname:nginx
#config:/opt/nginx/conf/nginx.conf
#pidfile:/comexHome/nginx/nginx.pid
#
#CreatedByhttp://comexchan.cnblogs.com/
#Sourcefunctionlibrary.
./etc/rc.d/init.d/functions
#Sourcenetworkingconfiguration.
./etc/sysconfig/network
#Checkthatnetworkingisup.
["$NETWORKING"="no"]&&exit0
NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"
prog=$(basename$NGINX_DAEMON_PATH)
start(){
[-x$NGINX_DAEMON_PATH]||exit5
[-f$NGINX_CONF_FILE]||exit6
echo-n$"Starting$prog:"
daemon$NGINX_DAEMON_PATH-c$NGINX_CONF_FILE
retval=$?
echo
[$retval-eq0]&&touch$NGINX_LOCK_FILE
return$retval
}
stop(){
echo-n$"Stopping$prog:"
killproc$prog-QUIT
retval=$?
echo
[$retval-eq0]&&rm-f$NGINX_LOCK_FILE
return$retval
}
restart(){
configtest||return$?
stop
start
}
reload(){
configtest||return$?
echo-n$"Reloading$prog:"
killproc$NGINX_DAEMON_PATH-HUP
RETVAL=$?
echo
}
force_reload(){
restart
}
configtest(){
$NGINX_DAEMON_PATH-t-c$NGINX_CONF_FILE
}
rh_status(){
status$prog
}
rh_status_q(){
rh_status>/dev/null2>&1
}
case"$1"in
start)
rh_status_q&&exit0
$1
;;
stop)
rh_status_q||exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q||exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q||exit0
;;
*)
echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac
上面的代码就是用来创建服务的代码,将他们保存在nginx4comex文件中(这个文件我仍在了/opt/nginx目录下,一样使用vim编写)。注意下面的代码和你的配置对应即可。
NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx" NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
接着我们继续终端指令操作。
chmodu+xnginx4comex cpnginx4comex/etc/init.d servicenginx4comexstatus vim/etc/rc.local /etc/init.d/nginx4comexstart reboot
最后来说我们已经设置了nginx代理tomcat,还设置了对应的服务器程序自启动。
注意!nginx4comex是不能被chkconfig的,具体原因我也不清楚,但是原作者的文章中确实用了chkconfig的方法加入了启动,对linux有兴趣的可以去试一试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。