linux下安装nginx及开机自启动

一、安装nginx

1.从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)

wget http://nginx.org/download/nginx-1.5.9.tar.gz

2.解压

tar -zxvf nginx-1.5.9.tar.gz 

3.设置一下配置信息,或者不执行此步,直接默认配置,与后面配置有关

./configure --prefix=/usr/local/nginx 

4.make 编译 (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

make

5.make install 安装 (make install是把这些编译出来的可执行文件和库文件复制到合适的地方)

make install

6.启动nginx

/usr/local/nginx/sbin/nginx

7.访问nginx,nginx默认端口为80

8.安装过程中可能会遇到的错误

(1)错误为:./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

yum -y install pcre-devel

(2)错误提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.

解决办法:

yum -y install openssl openssl-devel

二、nginx开机自启动

1.在linux系统的/etc/init.d/目录下创建nginx文件

vim /etc/init.d/nginx

2.在脚本中添加如下命令:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; thenuseradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d "=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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

3.如果你是自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:

nginx="/usr/local/nginx/sbin/nginx" 修改成nginx执行程序的路径。NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 修改成配置文件的路径。

4.保存脚本文件后设置文件的执行权限:

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

5.通过该脚本启动停止nginx服务

/etc/init.d/nginx start
/etc/init.d/nginx stop

6.使用chkconfig进行管理,将nginx服务加入chkconfig管理列表

chkconfig --add /etc/init.d/nginx

7.使用service对nginx进行启动,停止。重启等操作

service nginx start
service nginx stop

8.设置终端模式开机启动

chkconfig nginx on

三、发布静态资源

1.确定静态资源存放位置

/home/images

2.修改nginx.conf配置文件

vi /usr/local/nginx/conf/nginx.conf

3.主要修改如下:

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {
worker_connections  1024;
}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;charset utf-8;    #access_log  logs/host.access.log  main;location ~ .*\.(gif|jpg|jpeg|png)$ {    expires 24h;    root /home/images/;#指定图片存放路径    access_log /home/softs/nginx-1.5.9/logs/images.log;#日志存放路径    proxy_store on;    proxy_store_access user:rw group:rw all:rw;    proxy_temp_path         /home/images/;#图片访问路径    proxy_redirect          off;    proxy_set_header        Host 127.0.0.1;    client_max_body_size    10m;    client_body_buffer_size 1280k;    proxy_connect_timeout   900;    proxy_send_timeout      900;    proxy_read_timeout      900;    proxy_buffer_size       40k;    proxy_buffers           40 320k;    proxy_busy_buffers_size 640k;    proxy_temp_file_write_size 640k;    if ( !-e $request_filename)    {    proxy_pass  http://127.0.0.1;#默认80端口    }    
}      location / {root   /home/html; #html访问路径  index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}
}# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}}

4.查看编译是否出错,如果没出错则成功

cd /usr/local/nginx/sbin/
./nginx -t

5.显示如下即代表成功(如果报目录不存在错误,就创建目录即可)

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

6.访问静态资源:192.168.120.139/1.png

更多博客内容详见我的博客 Wang's Blog


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部