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