linux

Nginx的安装与配置

2019-04-27  本文已影响23人  dming1024

参考文章:
https://www.runoob.com/linux/nginx-install-setup.html
https://www.cnblogs.com/taiyonghai/p/6728707.html

一到周末,就有时间开始了自己的Linux之旅,这次尝试在自己的腾讯云服务器上安装Nginx,并进行简单的配置。

首先是安装Nginx的编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装 PCRE

cd /usr/local/src/
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

下面开始Nginx的安装

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make && make install

查看配置文件

cat 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  localhostx; #将服务器的名称改成了localhostx,端口80

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Nginx的配置
1.检查配置文件nginx.conf的正确性命令

/usr/local/webserver/nginx/sbin/nginx -t

2.启动Nginx

/usr/local/webserver/nginx/sbin/nginx

3.停止Nginx

/usr/local/webserver/nginx/sbin/nginx -s stop 

在启动Nginx之后,可以查看Nginx的运行状况

root     20360     1  0 14:52 ?        00:00:00 nginx: master process /usr/local/webserver/nginx/sbin/nginx
nobody   20361 20360  0 14:52 ?        00:00:00 nginx: worker process
root     22445 22185  0 15:13 pts/3    00:00:00 grep --color=auto nginx

如何在windows端口打开呢?
首先将端口加入云端防火墙白名单

vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state -- state NEW -m tcp --dport 80 -j ACCEPT
#保存之后退出

回到windows电脑,在“C:\Windows\System32\drivers\etc”下的hosts中配置一下域名重定向

203.195.154.241 localhostx #ip地址为你云服务器地址

在windows的命令对话框中,显示如下,说明配置成功

ping localhostx
正在 Ping localhostx [203.195.154.241] 具有 32 字节的数据:
来自 203.195.154.241 的回复: 字节=32 时间=44ms TTL=52
来自 203.195.154.241 的回复: 字节=32 时间=42ms TTL=52
来自 203.195.154.241 的回复: 字节=32 时间=56ms TTL=52
来自 203.195.154.241 的回复: 字节=32 时间=52ms TTL=52

203.195.154.241 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 42ms,最长 = 56ms,平均 = 48ms

利用telnet进行端口测试

telnet 203.195.154.241 80

出现如下结果说明host设置完成


Telnet.png

打开windows本地浏览器,输入localhostx,出现如下界面表示配置成功


Nginx.png
上一篇下一篇

猜你喜欢

热点阅读