本次实验实现目的:
安装Nginx,Nginx开启目录浏览、下载功能,开机默认启动;咐件自带开机启动脚本、重启脚本;
1、关闭SELINUX
查看获取SELinux的状态:
[root@localhost ~]# getenforce
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled #默认为:enforcing
2、添加开放nginx端口号
查看获取iptables的状态:
[root@localhost ~]# service iptables status
在防火墙IPtables 添加nginx监听的端口80;
[root@localhost ~]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
:wq #保存
重启IPtables
[root@localhost ~]# service iptables restart
确认安装成功并启动:nginx监听的端口80;
[root@localhost ~]# netstat -na |grep :80
3、下载软件包
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
[root@localhost src]# wget http://nginx.org/download/nginx-1.3.8.tar.gz
4、安装pcre 让安装Nginx支持rewrite 方便以后所需
方法1:
[root@localhost ~]# yum install pcre*
方法2:
[root@localhost src]# tar zxvf pcre-8.31.tar.gz
[root@localhost src]# cd pcre-8.31
[root@localhost pcre-8.31]# ./configure
[root@localhost pcre-8.31]# make;make install
5、安装nginx
将nginx-1.3.8.tar.gz传到/usr/local/src(安装需要编译的软件,最好放到这个目录下)。
[root@localhost src]# tar zxvf nginx-1.3.8.tar.gz
[root@localhost src]# cd nginx-1.3.8
[root@localhost nginx-1.3.8]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.3.8]# make;make install
6、安装Apache
[root@localhost ~]# yum install http*
[root@localhost ~]# service httpd restart
7、修改httpd配置文件
查找httpd.conf配置文件路径
[root@localhost ~]#find / -name httpd.conf
修改端口号:
修改端口号目的:httpd与nginx端口冲突,必须改其中一个端口号;
[root@localhost ~]#vim /etc/httpd/conf/httpd.conf
Listen 8080 #默认为:80,修改为:8080
8、nginx配置文件测试
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
测试成功!
Nginx: error while loading shared libraries: libpcre.so.1 报错:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
确认已经安装PCRE:
Shell代码
[root@localhost ~]# cd /lib
[root@localhost ~]# ls *pcre*
libpcre.so.0 libpcre.so.0.0.1
[root@localhost ~]# find / -type f -name *libpcre.so.*
添加软链接:
解决Shell代码 :(x86_bit)
[root@localhost ~]# ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1
前面在一般的linux上可以解决此问题.
注: 在有的操作系统上面,安装pcre后,安装的位置为/usr/local/lib/*pcre*
在redhat 64位机器之上有这样的情况.
在redhat 64位机器上, nginx可能读取的pcre文件为/lib64/libpcre.so.1文件.
所以在改用下面的软连接:
解决Shell代码 :(x64_bit)
[root@localhost ~]# ln -s /usr/local/lib/libpcre.so.1 /lib64/
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9、Nginx启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
出现这些东西,说明服务已经启动。
然后在ie中访问你服务器的ip,出现这个就安装成功了!
http://0.0.0.0
10、查看日志
[root@localhost ~]# tail /usr/local/nginx/logs/access.log
11、开启全站所有目录浏览功能
创建目录:
[root@localhost ~]# mkdir /webshare
编辑配置文件,在http{下面添加以下内容
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
autoindex on; #开启nginx目录浏览功能
autoindex_exact_size off; #文件大小从KB开始显示
autoindex_localtime on; #显示文件修改时间为服务器本地时间
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80; #Nginx端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /webshare; #/webshare网站根目录路径
autoindex on;
index index.html index.htm;
}
:wq #保存,退出
12、nginx开机自动启动
启动脚本
第一步、先运行命令关闭nginx
[root@localhost ~]# sudo kill `cat /usr/local/nginx/logs/nginx.pid`
第二步、编辑启动脚本、重启脚本(咐件有脚本,上传到/etc/init.d/目录下)
[root@localhost ~]# vim /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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/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
}
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
:wq 保存退出
第三步、设置/etc/init.d/nginx 为777权限
[root@localhost ~]# chmod 777 /etc/init.d/nginx
第四步、设置开机默认启动
[root@localhost ~]# /sbin/chkconfig nginx on
检查一下
[root@localhost ~]# sudo /sbin/chkconfig --list nginx
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
完成!
之后,就可以使用以下命令了;
service nginx start #开启
service nginx stop #停止
service nginx restart #重启
service nginx reload #重新加载
或
/etc/init.d/nginx start #开启
/etc/init.d/nginx stop #停止
/etc/init.d/nginx restart #重启
/etc/init.d/nginx reload #重新加载
然后在ie中访问你服务器的ip,出现这个就安装成功了!
http://0.0.0.0
上传一些资料到服务器上,客户端可以通IE下载;上传方法有多种,参考:
SecureCRT 安装上传(rz)和下载(sz) http://yanghuawu.blog.51cto.com/2638960/1009591
13、nginx关闭进程命令
方法一
停止操作是通过向nginx进程发送信号来进行的
步骤1:查询nginx主进程号
[root@localhost ~]# ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:
[root@localhost ~]# kill -QUIT 主进程号
快速停止Nginx:
[root@localhost ~]# kill -TERM 主进程号
强制停止Nginx:
[root@localhost ~]# kill -9 nginx
方法二
[root@localhost ~]# yum install lsof
[root@localhost ~]# lsof -i :80
[root@localhost ~]#kill -9 进程号
14、安装过程中问题
---------------------------------------------------------------------
有报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决方法:
[root@localhost nginx-1.3.8]# yum install pcre*
---------------------------------------------------------------------
安装完成:
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
----------------------------------------------------------------------
本文转自 yhw85 51CTO博客,原文链接:http://blog.51cto.com/yanghuawu/1055329,如需转载请自行联系原作者