服务器(ubuntu):Nginx安装与配置

1.安装gcc g++的依赖库

apt-get install build-essential
apt-get install libtool

2.安装pcre依赖库

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

3.安装zlib依赖库

apt-get install zlib1g-dev

4.安装ssl依赖库

apt-get install openssl

5.安装nginx

#下载最新版本:
wget http://nginx.org/download/nginx-1.11.3.tar.gz
#解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-http_v2_module
#编辑nginx:
make
注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx

6 . 配置(需要https功能的要申请ssl证书:https://help.aliyun.com/document_detail/98728.html? spm=a2c4g.11174359.6.582.105d842bpnaZPL)

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  servername;// 域名,多个空格隔开

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   /home/WWW;// 启动文件目录
           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       88;
       server_name  serverName;
       root   /home/mosowe;
       index  index.html index.htm;
       
   }


   # HTTPS server https协议配置
   
   server {
      listen       443 ssl; // #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
      server_name  serverName; // #将localhost修改为您证书绑定的域名,例如:www.example.com。

      ssl_certificate      xxxxx.pem; // 您证书的文件名如果不同目录需要加目录
      ssl_certificate_key  xxxxx.key; // 您证书的文件名

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

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

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

}

基础命令,进入nginx文件所在目录:

启动 ./nginx
停止 ./nginx -s stop
查看状态 ps -ef | grep nginx

你可能感兴趣的:(服务器)