一、安装依赖包
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make libtool
yum install -y libxslt* libxml2* gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
二、下载
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
三、编译
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-file-aio \
--with-http_realip_module \
--without-http_scgi_module \
--without-http_uwsgi_module \
--without-http_fastcgi_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_stub_status_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_auth_request_module \
--with-threads \
--with-stream \
--with-stream_ssl_module \
make && make install
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --with-http_addition_module --with-http_sub_module --with-http_stub_status_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_auth_request_module --with-threads --with-stream --with-stream_ssl_module
四、将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=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/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 二 2019-04-23 14:06:19 CST; 15s ago Process: 5873 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE) Process: 5869 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS) 4月 23 14:06:16 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e) 4月 23 14:06:17 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e) 4月 23 14:06:17 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e) 4月 23 14:06:18 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e) 4月 23 14:06:18 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e) 4月 23 14:06:19 node2.jinglong nginx[5873]: nginx: [emerg] still could not bind() 4月 23 14:06:19 node2.jinglong systemd[1]: nginx.service: control process exited, code=e...s=1 4月 23 14:06:19 node2.jinglong systemd[1]: Failed to start The NGINX HTTP and reverse pr...er. 4月 23 14:06:19 node2.jinglong systemd[1]: Unit nginx.service entered failed state. 4月 23 14:06:19 node2.jinglong systemd[1]: nginx.service failed. Hint: Some lines were ellipsized, use -l to show in full.
提示80端口被占用,此时执行:
[root@node2 sbin]# killall -9 nginx -bash: killall: 未找到命令
安装killall
yum install psmisc
再执行
killall -9 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 二 2019-04-23 14:24:54 CST; 17s ago Process: 6864 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE) Process: 6896 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS) Process: 6893 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS) Main PID: 6897 (nginx) CGroup: /system.slice/nginx.service ├─6897 nginx: master process /usr/local/nginx/sbin/nginx └─6898 nginx: worker process 4月 23 14:24:54 node2.jinglong systemd[1]: Starting The NGINX HTTP and reverse proxy server... 4月 23 14:24:54 node2.jinglong nginx[6893]: nginx: the configuration file /usr/local/ngin...ok 4月 23 14:24:54 node2.jinglong nginx[6893]: nginx: configuration file /usr/local/nginx/co...ul 4月 23 14:24:54 node2.jinglong systemd[1]: 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: 0 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可以在任意目录下执行命令