Linux——Centos下安装Nginx
1、安装依赖包
点击查看代码
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、下载并解压安装包
点击查看代码
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
3、安装nginx
点击查看代码
//进入nginx目录
cd /usr/local/nginx
//进入目录
cd nginx-1.13.7
//执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
4、启动nginx服务
点击查看代码
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
## 5、配置nginx.conf
点击查看代码
# 打开配置文件
vi /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
......
6、重启nginx
/usr/local/nginx/sbin/nginx -s reload
查看nginx进程是否启动:
点击查看代码
[[email protected] ~]# ps -ef |grep nginx
root 77065 1 0 15:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 77119 77065 0 15:23 ? 00:00:00 nginx: worker process
root 82236 36707 0 15:33 pts/1 00:00:00 grep --color=auto nginx
[[email protected] ~]#
7、防火墙设置
若想使用外部主机访问nginx,需要关闭服务器防火墙或开放nginx服务端口,端口为上一步nginx.conf的配置端口:
centOS6及以前版本使用命令: systemctl stop iptables.service
centOS7关闭防火墙命令: systemctl stop firewalld.service
** 关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口 :**
开放80端口:
firewall-cmd –zone=public –add-port=80/tcp –permanent
查询端口号80 是否开启:
firewall-cmd –query-port=80/tcp
重启防火墙:
firewall-cmd –reload
随后访问该ip:端口 即可看到nginx界面。
8、访问服务器ip查看
显示出 Welcome to nginx!表示安裝成功
9、常用命令
进入安装目录中,命令: cd /usr/local/nginx/sbin
启动
./nginx
关闭
./nginx -s stop
重启
./nginx -s reload
解决:nginx:未找到命令...
配置全局变量:
- 进入配置文件:vim /etc/profile
- 修改文件,在末尾处添加:
PATH=$PATH:/usr/local/nginx/sbin
export PATH - 刷新:source /etc/profile
- 查看版本:nginx -V
- 重启服务:nginx -s reload
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/279988.html