CentOS管理-Nginx+Apache环境

1. Nginx服务

1.1. 编译环境

#1.安装编译httpd的环境依赖
yum install -y gcc gcc-c++ pcre pcre-devel \
zlib zlib-devel openssl openssl-devel gd gd-devel

1.2. 编译安装

#1.下载nginx程序的tar包,解压至/opt/src目录
wget https://nginx.org/download/nginx-1.24.0.tar.gz  -P /opt/src
cd /opt/src && tar xf nginx-1.24.0.tar.gz

#2.创建nginx的日志目录,也用来存放守护进程pid,和temp缓存目录
mkdir -p /opt/apps/nginx/{logs,temp/{client_body,fastcgi,proxy,scgi,uwsgi}}

#3.编译nginx程序(安装目录/opt)
cd /opt/src/nginx-1.24.0
./configure --prefix=/opt/apps/nginx \
--user=nginx \
--group=nginx \
--error-log-path=/opt/apps/nginx/logs/error.log \
--http-log-path=/opt/apps/nginx/logs/access.log \
--pid-path=/opt/apps/nginx/logs/nginx.pid  \
--http-client-body-temp-path=/opt/apps/nginx/temp/client_body \
--http-fastcgi-temp-path=/opt/apps/nginx/temp/fastcgi \
--http-proxy-temp-path=/opt/apps/nginx/temp/proxy \
--http-scgi-temp-path=/opt/apps/nginx/temp/scgi \
--http-uwsgi-temp-path=/opt/apps/nginx/temp/uwsgi \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module

#4.构建nginx程序
make -j4 && make install

1.3. 管理服务

#1.创建软链接管理nginx版本
cd /opt/apps
mv nginx nginx-1.24.0
ln -s nginx-1.24.0 nginx

#2.创建nginx用户运行nginx服务
groupadd nginx && useradd nginx -g nginx -s /sbin/nologin -M

#3.赋权nginx应用目录权限至nginx用户
chown -R nginx:nginx /opt/apps/nginx*

#4.将httpd服务注册成系统的服务
cat <<EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
Environment="CONFFILE=/opt/apps/nginx/conf/nginx.conf"
ExecStart=/opt/apps/nginx/sbin/nginx  -c \$CONFFILE
ExecReload=/opt/apps/nginx/sbin/nginx -s reload
ExecStop=/opt/apps/nginx/sbin/nginx -s quit
PrivateTmp=true
Restart=always

[Install]
WantedBy=multi-user.target
EOF

#6.重新加载系统管理项
systemctl daemon-reload

1.4. 主机拆分

#1.修改nginx.cnf配置文件
vim /opt/apps/nginx/conf/nginx.conf
user  nginx;
worker_processes  8;

error_log  /opt/apps/nginx/logs/error.log;
pid       /opt/apps/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    include /opt/apps/nginx/conf/conf.d/*.conf;
    
    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  /opt/apps/nginx/logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    
}

#2.创建nginx的conf的子文件夹
mkdir -p /opt/apps/nginx/conf/conf.d

#3.创建nginx的子配置文件
tee > /opt/apps/nginx/conf/conf.d/nginx.conf <<EOF
server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
EOF

#4.重新赋权nginx应用目录权限至nginx用户
chown -R nginx:nginx /opt/apps/nginx*

#5.验证配置文件是否正确
[root@localhost conf]# /opt/apps/nginx/sbin/nginx -t
nginx: the configuration file /opt/apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/apps/nginx/conf/nginx.conf test is successful

1.5. 服务验证

#1.启动nginx服务
systemctl start nginx

#2.验证访问,正常返回HTTP/1.1 200 OK
curl -Is localhost| head -n 1

2. Apache服务

2.1. 编译环境

#1.安装编译httpd的环境依赖
yum install -y gcc gcc-c++ pcre pcre-devel \
zlib zlib-devel openssl openssl-devel gd gd-devel

2.2. 编译安装

#1.下载apr程序的tar包,解压至/opt/src目录。
wget https://archive.apache.org/dist/apr/apr-1.6.5.tar.gz  -P /opt/src
cd /opt/src && tar xf apr-1.6.5.tar.gz

#2.编译apr程序,安装apr-util的前置条件。
cd /opt/src/apr-1.6.5
./configure --prefix=/usr/local/apr
make -j4 && make install

#3.下载apr-util程序的tar包,解压至/opt/src目录。
wget https://archive.apache.org/dist/apr/apr-util-1.6.0.tar.gz  -P /opt/src
cd /opt/src && tar xf apr-util-1.6.0.tar.gz

#4.编译apr程序,安装httpd的前置条件。
cd /opt/src/apr-util-1.6.0
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make -j4 && make install

#5.创建/opt/apps目录
mkdir -p /opt/apps

#6.下载httpd程序的tar包,解压至/opt/src目录。
wget https://archive.apache.org/dist/httpd/httpd-2.4.57.tar.gz -P /opt/src
cd /opt/src && tar xf httpd-2.4.57.tar.gz

#7.编译httpd程序,依赖于apr和apr-util程序。
cd /opt/src/httpd-2.4.57
./configure --prefix=/opt/apps/httpd \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--with-pcre --with-zlib --with-mpm=prefork \
--enable-modules=all --enable-mpms-shared=all \
--enable-so --enable-rewrite --enable-ssl --enable-cgi
make -j4 && make install

2.3. 管理服务

#1.创建软链接管理httpd版本
cd /opt/apps
mv httpd httpd-2.4
ln -s httpd-2.4 httpd

#2.创建httpd用户运行apache
groupadd apache && useradd httpd -g apache -s /sbin/nologin -M

#3.赋权httpd应用目录权限至httpd用户
chown -R httpd:apache /opt/apps/httpd*

#4.修改httpd.cnf文件的运行用户
sed -i -e 's/User daemon/User httpd/g' /opt/apps/httpd/conf/httpd.conf
sed -i -e 's/Group daemon/Group apache/g' /opt/apps/httpd/conf/httpd.conf

#5.将httpd服务注册成系统的服务
cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
ExecStart=/opt/apps/httpd/bin/apachectl start
ExecReload=/opt/apps/httpd/bin/apachectl graceful
ExecStop=/opt/apps/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

#6.重新加载系统管理项
systemctl daemon-reload

2.4. 服务验证

#1.启动httpd服务
systemctl start httpd

#2.验证访问,正常返回HTTP/1.1 200 OK
curl -Is localhost| head -n 1  
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值