1. 设置nginx开机自动启动

这里使用的是编写shell脚本的方式来处理:

创建一个shell脚本nginx:

cd /etc/init.d
[root@winy init.d]# touch nginx
vi nginx 

打开nginx编辑,输入以下脚本内容:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

:wq  保存并退出


2. 设置文件的访问权限

chmod a+x /etc/init.d/nginx

(a+x ==> all user can execute  所有用户可执行)

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx

比如以下命令:
[root@winy init.d]# /etc/init.d/nginx status   --nginx运行状态
[root@winy init.d]# /etc/init.d/nginx restart  --重启
[root@winy init.d]# /etc/init.d/nginx start    --启动
[root@winy init.d]# /etc/init.d/nginx stop     --停止


3. 加入到rc.local文中

vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。


4.  配置文件服务器

cd /usr/local/nginx/conf
vi nginx.conf
http {
    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;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /home/ftpuser/www;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;

修改nginx.conf文件,上面标红的地方即可!


5. 测试

根据上面的nginx.conf配置文件可知,我的ftp目录为/home/ftpuser/www

那么我在www目录下面新建一个存放图片的目录images,

通过FileZilla 往images文件夹下添加一张图片hello.jpg,

那么在win7下通过浏览器url访问即可浏览到图片!!!!!

http://服务器ip/images/hello.jpg


注意点:

1. 防火墙设置

2. 之前有修改过nginx.conf文件,但是改坏了,而又没有手动备份,这时可以复制一份系统备份的文件

cp nginx.conf.default nginx.conf    ,然后再修改里面的标红部分!

3. win7访问的时候提示403 forbidden

调查方法一:

修改nginx.conf第一行user为root,默认是被注释掉的,放开

[root@winy conf]# vi nginx.conf
user  root;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


调查方法二:

修改ftp服务器文件夹的访问权限:

chmod 777 your_images_folder

对于我来说的话,这里的your_images_folder对应的我得ftp文件夹  /home/ftpuser/www




Logo

更多推荐