centos7编译安装nginx

时间:2022-06-07 09:07:06

一、安装依赖包

yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel

二、下载

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.14.2.tar.gz

三、编译

tar -zxvf nginx-1.14..tar.gz
cd nginx-1.14.
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

四、将nginx设置为服务

centos 7 中采用 systemd 来管理系统,我们为 nginx 创建服务文件

vim /lib/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

这里的 PIDFile 的配置要和前面的 配置保持一致。

设置自启动

[root@node2 logs]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

五、启动nginx

[root@node2 logs]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

提示报错,查看查看状态

[root@node2 logs]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 二 -- :: CST; 15s ago
Process: ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=/FAILURE)
Process: ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=/SUCCESS) 4月 :: node2.jinglong nginx[]: nginx: [emerg] bind() to 0.0.0.0: failed (...e)
4月 :: node2.jinglong nginx[]: nginx: [emerg] bind() to 0.0.0.0: failed (...e)
4月 :: node2.jinglong nginx[]: nginx: [emerg] bind() to 0.0.0.0: failed (...e)
4月 :: node2.jinglong nginx[]: nginx: [emerg] bind() to 0.0.0.0: failed (...e)
4月 :: node2.jinglong nginx[]: nginx: [emerg] bind() to 0.0.0.0: failed (...e)
4月 :: node2.jinglong nginx[]: nginx: [emerg] still could not bind()
4月 :: node2.jinglong systemd[]: nginx.service: control process exited, code=e...s=
4月 :: node2.jinglong systemd[]: Failed to start The NGINX HTTP and reverse pr...er.
4月 :: node2.jinglong systemd[]: Unit nginx.service entered failed state.
4月 :: node2.jinglong systemd[]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

提示80端口被占用,此时执行:

[root@node2 sbin]# killall - nginx
-bash: killall: 未找到命令

安装killall

yum install psmisc

再执行

killall - nginx

启动服务

[root@node2 sbin]# systemctl start nginx
[root@node2 sbin]#

没有报错,查看状态

[root@node2 sbin]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 二 -- :: CST; 17s ago
Process: ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=/FAILURE)
Process: ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=/SUCCESS)
Process: ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=/SUCCESS)
Main PID: (nginx)
CGroup: /system.slice/nginx.service
├─ nginx: master process /usr/local/nginx/sbin/nginx
└─ nginx: worker process 4月 :: node2.jinglong systemd[]: Starting The NGINX HTTP and reverse proxy server...
4月 :: node2.jinglong nginx[]: nginx: the configuration file /usr/local/ngin...ok
4月 :: node2.jinglong nginx[]: nginx: configuration file /usr/local/nginx/co...ul
4月 :: node2.jinglong systemd[]: Started The NGINX HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.

此时为running,启动成功,执行

[root@node2 sbin]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>

获取页面成功

建立软连接

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

此时nginx可以在任意目录下执行命令