nginx的安装与配置

^_^的我 7月前 ⋅ 246 阅读

登录服务器

ssh root@ip#输入密码登入终端
  1. 执行netstat -tunlp,如果显示相关服务的相关情况。则进行下一步,如果提示命令不存在则执行
yum install -y net-tools
  1. 安装nginx所需的依赖
yum install zlib zlib-devel openssl openssl-devel pcre-devel -y
yum install -y make gcc gcc-c++ -y
  1. 下载nginx

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

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

如果没有wget则先安装,安装命令

yum install -y wget

3.2 下载及安装配置

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
    --with-stream #流模块
make && make install

4.配置

4.1 首先要创建nginx的执行用户

groupadd www
useradd www -g www -s /sbin/nologin -d /www

4.2 进入nginx的配置文件目录

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

4.3 nginx配置文件讲解

#执行用户,上面已执行过创建操作
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;
}

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

mkdir /usr/local/nginx/conf/vhost

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

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

/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
BashCopy
#设置服务器重启自动重启

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

#查看自动重启项

chkconfig --list

配置完成后

浏览器端访问 http://ip

http://你的域名

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

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


全部评论: 0

    我有话说: