Linux——Centos下安装Nginx

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进程是否启动:

点击查看代码
[root@node1 ~]# 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
[root@node1 ~]# 

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:未找到命令...
配置全局变量

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native