linux安装nginx

查看是否安装 nginx
输入命令行: ps -ef | grep nginx

//tar -zxvf apache-tomcat-8.5.38.tar.gz

netstat -nat | grep ‘:80’

netstat -tunlp | grep 80

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
一、安装nginx

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

2.解压

tar -zxvf nginx-1.19.0.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`" ]; then
   useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
   if [ `echo $opt | grep '.*-temp-path'` ]; then
       value=`echo $opt | cut -d "=" -f 2`
       if [ ! -d "$value" ]; then
           # echo "creating" $value
           mkdir -p $value && chown -R $user $value
       fi
   fi
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;
upstream nginxWeb {
	server 192.168.1.180:8081;   #服务器地址1
	server 192.168.1.180:8082;   #服务器地址2
}

server {
    listen       80;
    server_name  116.62.15.47;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   D:\nginx\html\dist;
        index  index.html index.htm;
		proxy_pass http://nginxWeb/web/;
    }
	location /tlg {
        root   D:\nginx\html\dist;
        index  index.html index.htm;
		proxy_pass http://nginxWeb/tlg/;
    }
	location /PeterPan {
        root   D:\nginx\html\dist;
        index  index.html index.htm;
		proxy_pass http://nginxWeb/PeterPan/;
    }
	location /PeterPanTakeout {
        root   D:\nginx\html\dist;
        index  index.html index.htm;
		proxy_pass http://nginxWeb/PeterPanTakeout/;
    }
    #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.1.180

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux安装Nginx有几种方法。一种方法是通过包管理器进行安装。您可以使用命令"yum install nginx"来安装最新的稳定版本,默认情况下安装的是Nginx 1.20.2版本。另外一种方法是通过源码编译安装Nginx。这需要下载Nginx安装包并解压缩,然后进行依赖安装和配置。具体步骤如下: 1. 下载Nginx安装包,并解压缩。 2. 安装Nginx的依赖包。 3. 进入解压缩后的Nginx目录,执行"./configure"命令进行配置。 4. 执行"make"命令进行编译。 5. 执行"make install"命令进行安装。 6. 修改Nginx的配置文件"nginx.conf",可以设置用户和用户组等参数。 7. 启动Nginx,可以使用命令"nginx"。 8. 停止或重启Nginx,可以使用命令"nginx -s stop"和"nginx -s reload"。 9. 设置Nginx开机自启动,可以将Nginx添加到系统的启动项中。 10. 配置防火墙,确保80端口开放以允许Nginx的HTTP访问。 根据您的需求和喜好,您可以选择适合您的安装方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux安装nginx详细步骤](https://blog.csdn.net/adaizzz/article/details/126669430)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [linux 系统下四种nginx安装方法](https://blog.csdn.net/shallow72/article/details/123878716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值