本次實驗實現目的:
安裝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
測試成功!
[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; #顯示文件修改時間為服務器本地時間
charset utf-8,gbk; #顯示中文
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"
----------------------------------------------------------------------
本文出自 “運維工作奮斗” 博客,請務必保留此出處http://yanghuawu.blog.51cto.com/2638960/1055329