1. 安装Nginx
用tar命令解压文件的时候出错了,信息如下:
tar (child): lbzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
解决方法很简单,只要安装bzip2就行了,yum安装的命令如下:
[root@localhost]# yum -y install bzip2
在安装nginx时出现以下错误:
[root@centos6-1 ~]# yum install nginx -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: mirrors.shu.edu.cn
No package nginx available.
Error: Nothing to do
[root@host ~]# vim /etc/yum.repos.d/nginx.repo
# 在文件中写入以下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
退出保存后,直接安装:
[root@host ~]# yum install nginx -y
2. 查找nginx配置文件
[root@host ~]# find / -name nginx.conf
利用vim /etc/nginx/conf.d/nginx.conf
修改配置文件内容为
user www www;
worker_processes auto;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server_tokens off;
access_log off;
server {
listen 80;
server_name ptmicky.tk;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
按ESC,再输入 :wq
保存退出
使用查找命令查找niginx,然后在启动Nginx服务前检查配置文件语法
[root@host ~]# find / -name nginx
[root@host ~]# /application/nginx/sbin/nginx -t
启动Nginx
/etc/init.d/nginx start
# 启用1
/etc/init.d/nginx restart
# 重启
nginx
# 启用2
查看已开启的端口信息
[root@host ~]# netstat -lnp | grep 80
可以看到Nginx已经在侦听80端口。
查看Nginx进程
[root@host ~]# ps aux | grep nginx
在这里查看到nginx的应用程序是在哪里,配置文件具体是用哪一个
在主机浏览器上输入域名或者IP可以看到测试结果。
使用wget命令和curl命令测试
[root@host ~]# wget 127.0.0.1
[root@host ~]# curl 127.0.0.1
测试html网页
[root@host ~]# curl localhost
# 这里可以检测网页是否仍为“lnmp的一键安装首页”
3. 故障排查<Nginx启动失败,80端口已被占用>
检查Nginx配置
[root@host ~]# ./sbin/nginx -t -c ./conf/nginx.conf
nginx: the configuration file /usr/local/nginx/./conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/./conf/nginx.conf test is successful
不依靠残留进程,重新启动Nginx
[root@host ~]# /usr/local/nginx/sbin/nginx -c
[root@host ~]# /usr/local/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
强制性杀死进程
[root@host ~]# pkill -9 nginx
[root@host ~]# /usr/local/nginx/sbin/nginx -c
[root@host ~]# /usr/local/nginx/conf/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
再次启用nginx
[root@host ~]# /etc/init.d/nginx start
Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [FAILED]
查看Nginx服务状态
[root@host ~]# systemctl status nginx.service
â— nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
Loaded: loaded (/etc/rc.d/init.d/nginx)
Active: failed (Result: exit-code) since Tue 2018-01-02 23:19:52 CST; 38s ago
Docs: man:systemd-sysv-generator(8)
Process: 15553 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)
Jan 02 23:19:50 i-14fe7093 nginx[15553]: nginx: [emerg] bind() to 0.0.0.0:80...)
Jan 02 23:19:52 i-14fe7093 nginx[15553]: nginx: [emerg] still could not bind()
Jan 02 23:19:52 i-14fe7093 nginx[15553]: [FAILED]
Jan 02 23:19:52 i-14fe7093 systemd[1]: nginx.service: control process exite...=1
Jan 02 23:19:52 i-14fe7093 systemd[1]: Failed to start SYSV: Nginx is an HT...r.
Jan 02 23:19:52 i-14fe7093 systemd[1]: Unit nginx.service entered failed state.
Jan 02 23:19:52 i-14fe7093 systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
查看系统日志
[root@host ~]# journalctl -xe
Jan 02 23:19:49 i-14fe7093 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Unit nginx.service has begun starting up.
Jan 02 23:19:50 i-14fe7093 nginx[15553]: Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 02 23:19:52 i-14fe7093 nginx[15553]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 02 23:19:52 i-14fe7093 nginx[15553]: nginx: [emerg] still could not bind()
Jan 02 23:19:52 i-14fe7093 nginx[15553]: [FAILED]
Jan 02 23:19:52 i-14fe7093 systemd[1]: nginx.service: control process exited, code=exited status=1
Jan 02 23:19:52 i-14fe7093 systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
--Support:http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Jan 02 23:19:52 i-14fe7093 systemd[1]: Unit nginx.service entered failed state.
Jan 02 23:19:52 i-14fe7093 systemd[1]: nginx.service failed.
Jan 02 23:19:52 i-14fe7093 polkitd[1300]: Unregistered Authentication Agent for unix-process:15548:1690636 (system bus name :1.71, object path /org/freedesktop/PolicyKi
Jan 02 23:19:52 i-14fe7093 root[15630]: [euid=root]:root pts/0 2018-01-02 23:07 (10.247.123.130):[/usr/local/nginx]2018-01-02 23:19:49 root /etc/init.d/nginx start
Jan 02 23:20:01 i-14fe7093 CROND[15632]: (root) CMD (/sbin/ntpdate pool.ntp.org > /dev/null 2>&1)
Jan 02 23:20:31 i-14fe7093 root[15675]: [euid=root]:root pts/0 2018-01-02 23:07 (10.247.123.130):[/usr/local/nginx]2018-01-02 23:20:31 root systemctl status nginx.servi
Jan 02 23:20:58 i-14fe7093 root[15685]: [euid=root]:root pts/0 2018-01-02 23:07 (10.247.123.130):[/usr/local/nginx]2018-01-02 23:20:31 root systemctl status nginx.servi
-- Logs begin at Tue 2018-01-02 18:38:04 CST, end at Tue 2018-01-02 23:20:58 CST. --
Jan 02 23:19:49 i-14fe7093 systemd[1]: Starting SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has begun starting up.
Jan 02 23:19:50 i-14fe7093 nginx[15553]: Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 02 23:19:52 i-14fe7093 nginx[15553]: nginx: [emerg] still could not bind()
Jan 02 23:19:52 i-14fe7093 nginx[15553]: [FAILED]
Jan 02 23:19:52 i-14fe7093 systemd[1]: nginx.service: control process exited, code=exited status=1
Jan 02 23:19:52 i-14fe7093 systemd[1]: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Jan 02 23:20:01 i-14fe7093 CROND[15632]: (root) CMD (/sbin/ntpdate pool.ntp.org > /dev/null 2>&1)
Jan 02 23:20:31 i-14fe7093 root[15675]: [euid=root]:root pts/0 2018-01-02 23:07 (10.247.123.130):[/usr/local/nginx]2018-01-02 23:20:31 root systemctl status nginx.servi
Jan 02 23:20:58 i-14fe7093 root[15685]: [euid=root]:root pts/0 2018-01-02 23:07 (10.247.123.130):[/usr/local/nginx]2018-01-02 23:20:31 root systemctl status nginx.servi
-lt 列出监听的TCP端口
-u 或 --udp 显示UDP传输协议的连线状况
-n 输出中不解析主机、端口、名称
-p 显示进程ID和名称
[root@host ~]# netstat -ltunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 1065/memcached
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1005/redis-server 1
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 816/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 958/master
tcp 0 0 10.247.110.35:95 0.0.0.0:* LISTEN 891/python
tcp6 0 0 :::8080 :::* LISTEN 1160/java
tcp6 0 0 :::80 :::* LISTEN 812/httpd
tcp6 0 0 :::22 :::* LISTEN 816/sshd
tcp6 0 0 ::1:25 :::* LISTEN 958/master
tcp6 0 0 :::443 :::* LISTEN 812/httpd
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 1160/java
udp 0 0 127.0.0.1:11211 0.0.0.0:* 1065/memcached
udp 0 0 0.0.0.0:68 0.0.0.0:* 756/dhclient
udp 0 0 0.0.0.0:28965 0.0.0.0:* 756/dhclient
udp6 0 0 :::50624 :::* 756/dhclient
杀死指定PID的进程
[root@host ~]# kill 812
检验进程是否杀死
[root@host ~]# netstat -ltunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 1065/memcached
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1005/redis-server 1
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 816/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 958/master
tcp 0 0 10.247.110.35:95 0.0.0.0:* LISTEN 891/python
tcp6 0 0 :::8080 :::* LISTEN 1160/java
tcp6 0 0 :::22 :::* LISTEN 816/sshd
tcp6 0 0 ::1:25 :::* LISTEN 958/master
tcp6 0 0 127.0.0.1:8006 :::* LISTEN 1160/java
udp 0 0 127.0.0.1:11211 0.0.0.0:* 1065/memcached
udp 0 0 0.0.0.0:68 0.0.0.0:* 756/dhclient
udp 0 0 0.0.0.0:28965 0.0.0.0:* 756/dhclient
udp6 0 0 :::50624 :::* 756/dhclient
重启Nginx,此时没有残留进程占用80端口了
[root@host ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf