配置静态服务器与nginx的安装与配置

okgoes 2023-05-09 13:07:19
Categories: Tags:

centos服务器可用

登录远端服务器

1
ssh root@ip#输入密码登入终端

执行netstat -tunlp

如果显示相关服务的相关情况。则进行下一步,如果提示命令不存在则执行

1
yum install -y net-tools

配置静态服务器

可以用的服务软件有apache、nginx等,这里以nginx为例。nginx的安装相关准备如下:

首先先安装nginx所需的依赖

#安装nginx依赖

1
2
yum install zlib zlib-devel openssl openssl-devel pcre-devel -y
yum install -y make gcc gcc-c++ -y

下载nginx

nginx下载地址:http://nginx.org/en/download.html

我们可以通过wget下载,当然首先要先安装wget

#如果没有wget则执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
yum install -y wget 
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#nginx安装另一个依赖pcre只要解压不需安装
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
tar -zxvf pcre-8.40.tar.gz
#下载完成后解压
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure \
    --prefix=/usr/local/nginx \
    --sbin-path=/usr/local/nginx/sbin/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/nginx.pid \
    --with-http_ssl_module \
    --with-pcre= \
    --with-zlib= \
    --error-log-path=/data/nginx/error/error.log \
    --http-log-path=/data/nginx/error/http.log \
    --user=www \
    --group=www \
    --lock-path=/var/lock/nginx.lock \
    --with-http_ssl_module \
    --with-http_flv_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/tmp/nginx/client/ \
    --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    --http-scgi-temp-path=/var/tmp/nginx/scgi \
    --with-pcre=../pcre-8.40
make && make install

配置

首先要创建nginx的执行用户

1
2
groupadd www
useradd www -g www -s /sbin/nologin

然后进入nginx的配置文件目录

1
2
3
cd /usr/local/nginx/conf
#修改nginx配置文件
vim nginx.conf

nginx配置文件讲解

#执行用户,上面已执行过创建操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
user  www;
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;
    server_tokens off;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #配置最大请求资源大小
    client_max_body_size 40m;
    #开启gzip压缩
    gzip  on;
    server {
        listen       80#监听端口
        server_name  localhost; #配置域名
        root           /usr/local/nginx/html; #资源路径
        #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$ {
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        #配置php静态服务不配置
        location ~ .*\.(php|php5)?$  {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
        # 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;
    #    }
    #}
    #引入vhost目录下的.conf文件
    include vhost/*.conf;
}

创建nginx配置存放目录vhost,上面配置文件中已提到

1
mkdir /usr/local/nginx/conf/vhost

#创建完成后,我们可以把每个网站的配置文件都放在/usr/local/nginx/conf/vhost

#执行以下命令进行配置文件的检测

1
2
3
4
5
6
7
8
/usr/local/nginx/sbin/nginx -t
#如果配置成功则提示
#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服务
/usr/local/nginx/sbin/nginx
#重启命令为
/usr/local/nginx/sbin/nginx -s reload

#设置服务器重启自动重启

1
2
cp /usr/local/nginx/sbin/nginx /etc/init.d/nginx
chkconfig --add nginx

#查看自动重启项

1
chkconfig --list

配置完成后

浏览器端访问 http://ip

http://你的域名

域名需要解析到你的服务器才行

note 如果不能访问,则要检查是否开放了80端口或者监听是否是80端口,如果是阿里云服务器则检查安全组配置是否正确。