您现在的位置是: 首页> 学无止境> Linux> Linux

Linux下安装nginx

张伟江2019-01-14 10:09Linux2790人已围观

简介Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

1、创建www用户组和www用户

user=www
group=www
#create group if not exists
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
fi

#create user if not exists
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -g $group -s /sbin/nolgin $user
fi

2、下载并解压nginx安装包

rm -rf nginx-1.12.2
if [ ! -f nginx-1.12.2.tar.gz ];then
  #wget http://nginx.org/download/nginx-1.12.2.tar.gz
  wget https://f.9635.com.cn/linux/nginx-1.12.2.tar.gz
fi
tar zxvf nginx-1.12.2.tar.gz

3、编译和安装

cd nginx-1.12.2
./configure --user=www \
--group=www \
--prefix=/alidata/server/nginx \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_gzip_static_module
CPU_NUM=$(cat /proc/cpuinfo | grep processor | wc -l)
if [ $CPU_NUM -gt 1 ];then
    make -j$CPU_NUM
else
    make
fi
make install

4、设置nginx日志文件和.conf文件的配置所在目录

chmod 775 /alidata/server/nginx/logs
chown -R www:www /alidata/server/nginx/logs
chmod -R 775 /alidata/www
chown -R www:www /alidata/www
cd ..
cp -fR ./nginx/config-nginx/* /alidata/server/nginx/conf/
cp -fR ./nginx/conf/nginx.conf /alidata/server/nginx/conf/
sed -i 's/worker_processes  2/worker_processes  '"$CPU_NUM"'/' /alidata/server/nginx/conf/nginx.conf

5、添加nginx到service自启动

chmod 755 /alidata/server/nginx/sbin/nginx
#/alidata/server/nginx/sbin/nginx
mv /alidata/server/nginx/conf/nginx /etc/init.d/
chmod +x /etc/init.d/nginx

6、设置nginx开机自启动

# 设置开机启动
cat > /usr/lib/systemd/system/nginx.service<<"EOF"
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/nginx start
ExecReload=/etc/rc.d/init.d/nginx reload
ExecStop=/etc/rc.d/init.d/nginx stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx.service
systemctl start nginx.service
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3


完整脚本如下:

#!/bin/bash
# lnmp之nginx1.12.2的安装
# author ctocode-zwj <982215226@qq.com>

# 判断用户或用户组是否存在,不存在则创建
user=www
group=www
#create group if not exists
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
fi

#create user if not exists
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -g $group -s /sbin/nolgin $user
fi

rm -rf nginx-1.12.2
if [ ! -f nginx-1.12.2.tar.gz ];then
  #wget http://nginx.org/download/nginx-1.12.2.tar.gz
  wget https://f.9635.com.cn/linux/nginx-1.12.2.tar.gz
fi
tar zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --user=www \
--group=www \
--prefix=/alidata/server/nginx \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_gzip_static_module
CPU_NUM=$(cat /proc/cpuinfo | grep processor | wc -l)
if [ $CPU_NUM -gt 1 ];then
    make -j$CPU_NUM
else
    make
fi
make install
chmod 775 /alidata/server/nginx/logs
chown -R www:www /alidata/server/nginx/logs
chmod -R 775 /alidata/www
chown -R www:www /alidata/www
cd ..
cp -fR ./nginx/config-nginx/* /alidata/server/nginx/conf/
cp -fR ./nginx/conf/nginx.conf /alidata/server/nginx/conf/
sed -i 's/worker_processes  2/worker_processes  '"$CPU_NUM"'/' /alidata/server/nginx/conf/nginx.conf
chmod 755 /alidata/server/nginx/sbin/nginx
#/alidata/server/nginx/sbin/nginx
mv /alidata/server/nginx/conf/nginx /etc/init.d/
chmod +x /etc/init.d/nginx
# 设置开机启动
cat > /usr/lib/systemd/system/nginx.service<<"EOF"
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/init.d/nginx start
ExecReload=/etc/rc.d/init.d/nginx reload
ExecStop=/etc/rc.d/init.d/nginx stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx.service
systemctl start nginx.service

github示例:https://github.com/zhangweijiang/linux/blob/master/shell/lnmp-nginx-1.12.2.sh


- END -

点赞(0) 打赏

上一篇:Linux下安装redis

下一篇:linux下安装php

相关文章

文章评论 共有 0 条评论

暂无评论

站点信息

  • 建站时间:2018年10月24日
  • 网站程序:fastadmin
  • 文章统计301篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信号:zwj982215226

打赏本站

  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!
  • 微信扫码:你说多少就多少~
  • 支付宝扫码:你说多少就多少~

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部