008-centos6.5搭建web服务【nginx-tomcat8-jre8】
阅读原文时间:2023年07月09日阅读:3

yum install vim

ulimit -n和-u可以查看linux的最大进程数和最大文件打开数。

ulimit -a 展示所有

临时方法:

  为了优化linux性能,可能需要修改这个最大值。临时修改的话ulimit -n 204800就可以了,重启后失效。

永久生效的方法:

  修改vim /etc/security/limits.conf文件

  在文件末尾添加:

* soft nofile
* hard nofile
* soft nproc
* hard nproc

按键盘esc后,键盘 :后,wq    重启查看校验

  * 代表针对所有用户

  noproc 是代表最大进程数

  nofile 是代表最大文件打开数

然后查看即可

  如遇有修改不成功linux系统

  可以尝试修改:/etc/security/limits.d/90-nproc.conf文件尾添加

* soft nproc
* hard nproc

  /etc/security/limits.d/def.conf文件尾添加

* soft nofile
* hard nofile

  这两个文件的设置将会覆盖前面的设置。重启后生效  

useradd admin 不指定的话,会默认创建同名组(默认就行)

mkdir -p /export/servers

下载放置:/export/servers/下,安装位置均在此目录

下载放置:/export/servers/下

wget https://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.54/bin/apache-tomcat-8.5.54.tar.gz
tar -zvxf apache-tomcat-8.5.54.tar.gz
mv apache-tomcat-8.5.54 tomcat8.5.54

scp /Users/lihongxu6/tmp/jdk-8u111-linux-x64.tar.gz root@domainip:/export/servers/jdk-8u111-linux-x64.tar.gz
tar -zvxf jdk-8u111-linux-x64.tar.gz

vim /etc/profile

JAVA_HOME=/export/servers/jdk1..0_111
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

nginx的编译需要c++,同时prce(重定向支持)和openssl(https支持)也需要安装。

yum -y install gcc-c++ pcre* openssl*

# 下载
wget http://nginx.org/download/nginx-1.15.12.tar.gz

解压:

tar -zvxf nginx-1.15.12.tar.gz
cd nginx-1.15.12

指定目录安装

./configure --prefix=/export/servers/nginx
make
make install

1、配置修改【扩展自定义】

创建

mkdir -p /export/servers/nginx/run

修改权限

chown -R admin:admin /export

chmod -R 777 /export/servers

2、启动权限修改,因为非root用户只能使用 1024 以上。原因是:默认情况下Linux的1024以下端口是只有root用户才有权限占用

方法一:

cd /export/servers/nginx/sbin

所有用户都可以运行(因为是755权限,文件所有者:root,组所有者:root)

chown root.root nginx
chmod 755 nginx
chmod u+s nginx

方法二:

仅 root 用户和 lhx  用户可以运行(因为是750权限,文件所有者:root,组所有者:admin)

chown root.www nginx
chmod 750 nginx
chmod u+s nginx

3、修改conf 下的nginx.conf

vim /export/servers/nginx/conf/nginx.conf

清空:在命令模式下,首先执行  gg 这里是跳至文件首行 再执行:dG 这样就清空了整个文件

user admin admin;
worker_processes ;
#worker_cpu_affinity ;
error_log /export/servers/nginx/logs/nginx_error.log warn;
pid /export/servers/nginx/run/nginx.pid;
worker_rlimit_nofile ;
events
{
use epoll;
worker_connections ;
}
http
{
include mime.types;
default_type application/octet-stream;
server_tokens on;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
#charset utf-;
server_names_hash_bucket_size ;
client_header_buffer_size 32k;
large_client_header_buffers 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout ;
tcp_nodelay on;
client_body_buffer_size 512k;
fastcgi_intercept_errors on;
proxy_connect_timeout ;
proxy_read_timeout ;
proxy_send_timeout ;
proxy_buffer_size 256k;
proxy_buffers 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_intercept_errors on;
server_name_in_redirect off;
proxy_hide_header X-Powered-By;

    gzip                            on;  
    gzip\_min\_length                 ;  
    gzip\_buffers                     16k;  
    gzip\_http\_version               1.0;  
    gzip\_comp\_level                 ;  
    gzip\_types                      text/plain application/x-javascript text/css application/xml;  
    gzip\_vary                       on;  
    error\_page                  = http://www.jd.com/error2.aspx;

include domains/*;
########default,deny ip access#####
server
{
listen 80 default_server;
server_name localhost;
location / {
rewrite ^/(.*) http://www.jd.com permanent;
}
}
###########status#########

server

{

listen 80;

server_name status.360buy.com;

location / {

stub_status on;

access_log off;

}

}

}

进入安装目录sbin,后 ./nginx 即可
查看进程:ps -ef | grep nginx
重启:./nginx -s reload

在/etc/init.d/目录下创建nginx文件

vim /etc/init.d/nginx

#!/bin/sh

nginx - this script starts and stops the nginx daemon

chkconfig: -

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

指定nginx执行文件路径

nginx="/usr/local/nginx/sbin/nginx"

nginx="/export/servers/nginx/sbin/nginx"
prog=$(basename $nginx)

指定nginx配置文件路径

#NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_CONF_FILE="/export/servers/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V >& | 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 >& | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f `
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit
[ -f $NGINX_CONF_FILE ] || exit
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep
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 >&
}
case "$1" in
start)
rh_status_q && exit
$
;;
stop)
rh_status_q || exit
$
;;
restart|configtest)
$
;;
reload)
rh_status_q || exit
$
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit
esac

设置/etc/init.d/nginx 执行权限

chmod 777 /etc/init.d/nginx

设置开机默认启动

chkconfig --add nginx //添加系统服务
chkconfig --level nginx on //设置开机启动,启动级别
chkconfig --list nginx //查看开机启动配置信息

nginx 控制命令

service nginx start #开启
service nginx stop #停止
service nginx restart #重启
service nginx reload #重新加载

参看:https://docs.jdcloud.com/cn/devops/install-agent

curl -fsSL https://deploy-code-vpc.jdcloud.com/dl-ifrit-agents/install | bash -s zero-agent,hawkeye-agent,log-agent,ark-query

使用命令查看安装情况

ps -ef|grep agent

四、注意事项

1、NAT-组网后

负载均衡器:网络选择应该选择NAT网络,否则会有 访问外部时候可以,外部无法访问站点