centos7使用yum安装nginx
1,查看系统版本
# 查看内核版本
[zhangjun.xue ~]$ uname -r
3.10.0-862.2.3.el7.x86_64
# 查看系统版本
[zhangjun.xue ~]$ cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
2,使用以下yum
命令安装Nginx :(需要root权限,加sudo)
sudo yum -y install nginx
3,启动nginx
[zhangjun.xue ~]$ sudo systemctl start nginx
[zhangjun.xue ~]$ ps -ef | grep nginx
root 9169 1 0 14:59 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 9170 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9171 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9172 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9173 9169 0 14:59 ? 00:00:00 nginx: worker process
zhangju+ 9180 8978 0 14:59 pts/0 00:00:00 grep --color=auto nginx
如果您正在运行防火墙,请运行以下命令以允许HTTP和HTTPS通信:(如果防火墙关了,可直接跳过)
4,设置防火墙
允许http通信
sudo firewall-cmd --permanent --zone=public --add-service=http
允许https通信
sudo firewall-cmd --permanent --zone=public --add-service=https
重启防火墙
sudo firewall-cmd --reload
5,测试
浏览器里键入你的主机地址:即ip
如果看到这个页面,那么你的Web服务器现在已经正确安装了。
如果想在系统启动时启用Nginx。请输入以下命令:
# 设置nginx开机自启
[zhangjun.xue ~]$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 查看nginx状态
[zhangjun.xue ~]$ 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-06-19 14:59:09 CST; 8min ago
Main PID: 9169 (nginx)
CGroup: /system.slice/nginx.service
├─9169 nginx: master process /usr/sbin/nginx
├─9170 nginx: worker process
├─9171 nginx: worker process
├─9172 nginx: worker process
└─9173 nginx: worker process
恭喜你!!! Nginx现在已经成功安装并运行了!!!
查看nginx进程
[zhangjun.xue nginx]$ ps -ef | grep nginx
root 9169 1 0 14:59 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 9170 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9171 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9172 9169 0 14:59 ? 00:00:00 nginx: worker process
nginx 9173 9169 0 14:59 ? 00:00:00 nginx: worker process
zhangju+ 10439 8978 0 15:16 pts/0 00:00:00 grep --color=auto nginx
上面的输出,master process后面的 : /usr/sbin/nginx 即nginx文件的位置
查找nginx配置文件位置
[zhangjun.xue nginx]$ sudo find / -name nginx.conf
/etc/nginx/nginx.conf
* 将nginx添加到系统环境变量
# 查找安装路径:
whereis nginx
# 将安装路径添加到环境变量
vim ~/.bashrc
在末尾加上一句,路径即whereis找到的路径
export PATH=$PATH:/usr/sbin/nginx
# 保存退出
source ~/.bashrc
# 然后可以直接使用命令启动和停止nginx
# 启动nginx
nginx
# 停止
nginx -s stop
# 重启
nginx -s reload
# 等待nginx进程处理任务结束后停止
nginx -s quit
# 相当于是先查出nginx的进程id,然后使用kill命令强制杀掉进程
# nginx在服务器上的默认端口是:80
* 查看nginx版本
[zhangjun.xue nginx]$ nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_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_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
* 检查nginx配置
[zhangjun.xue/]$ nginx -t
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2019/06/19 15:57:20 [warn] 13202#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2019/06/19 15:57:20 [emerg] 13202#0: open() "/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
可见,报错了,
2019/06/19 15:57:20 [emerg] 13202#0: open() "/run/nginx.pid" failed (13: Permission denied)
先这样了,哈哈哈哈哈!!!