Centos7 下源码安装nginx

一、安装必要的库:
(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库) 选定/usr/local/nginx 为安装目录 ,文中版本号可根据实际情况改变。

1、安装gcc gcc-c++:

  yum install -y gcc gcc-c++

2、安装PCER库:

cd /usr/local/nginx      //进入安装目录
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz      //下载安装包
tar -zxvf pcre-8.36.tar.gz              // 解压
cd pcre-8.33
./configure
make && make install

3、安装SSL:

cd  /usr/local/nginx
wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
tar -zxvf openssl-1.0.1j.tar.gz
cd openssl-1.0.1j
./config
make && make install

4、安装zlib库:

cd /usr/local/nginx
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
./configure
make && make install

二、安装nginx:

1、安装:

cd /usr/local/nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --user=www --group=www --prefix=/usr/local/nginx/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module

(*注: --with-http_ssl_module:这个不配置,在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常*)

 make && make install

2、启动:

/usr/local/nginx/nginx/sbin/nginx

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

3、重启停止命令:

3.1、重启:

/usr/local/nginx/nginx/sbin/nginx –s reload

3.2、停止:

/usr/local/nginx/nginx/sbin/nginx –s stop

3.3、强制关闭:

pkill nginx

3.4、测试配置文件是否正常

/usr/local/nginx/nginx/sbin/nginx –t

三、开机启动:
3.1、进入/lib/systemd/system目录

cd /lib/systemd/system

3.2、创建nginx.server文件

vi nginx.server

添加如下内容:

[Unit]
Description=nginx service
After=network.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
   
[Install] 
WantedBy=multi-user.target

参数说明:

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

3.3、nginx启动、停止、刷新

# systemctl start nginx.service          启动nginx服务

# systemctl stop nginx.service           停止服务

# systemctl restart nginx.service        重新启动服务

# systemctl list-units --type=service     查看所有已启动的服务

# systemctl status nginx.service          查看服务当前状态

# systemctl enable nginx.service          设置开机自启动

# systemctl disable nginx.service         停止开机自启动

四、遇到的错误以及解决方法:

3.1、SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option.
解决方法:

yum -y install openssl openssl-devel

3.2、[emerg] getpwnam("www") failed
解决方法:错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www

/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在CentOS 7下源码安装Nginx的步骤: 1. 安装必要的依赖包: ``` sudo yum install -y gcc gcc-c++ make automake autoconf libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel ``` 2. 下载并解压Nginx源码包: ``` wget http://nginx.org/download/nginx-1.18.0.tar.gz tar -zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0 ``` 3. 配置编译参数: ``` ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module ``` 以上参数的含义: - `--prefix=/usr/local/nginx`:指定Nginx安装目录为`/usr/local/nginx` - `--with-http_ssl_module`:启用SSL功能 - `--with-http_v2_module`:启用HTTP/2功能 4. 编译安装: ``` make && make install ``` 5. 启动Nginx: ``` /usr/local/nginx/sbin/nginx ``` 6. 验证Nginx是否已经成功启动: 在浏览器中访问服务器IP地址或域名,如果能够看到"Welcome to nginx!"的页面,说明Nginx已经成功安装并启动了。 7. 将Nginx设置为系统服务(可选): ``` sudo vi /etc/systemd/system/nginx.service ``` 将以下内容复制到文件中: ``` [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target ``` 保存并退出。 使用以下命令重新加载systemd配置: ``` sudo systemctl daemon-reload ``` 使用以下命令启动Nginx服务: ``` sudo systemctl start nginx ``` 使用以下命令设置开机启动: ``` sudo systemctl enable nginx ``` 至此,在CentOS 7下源码安装Nginx完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值