Linux中nginx安裝基礎教程
1.安裝所需的運行環境
(1) 需要安裝gcc的環境。
yum install gcc-c++
(2)第三方包。
Perl庫
PCRE(Perl Compatible Regular Expressions), perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要 在linux上安裝pcre庫。
yum install -y pcre pcre-devel
zlib庫
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試 或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl openssl-devel
2.安裝部署NGINX
(1)下載NGINX
NGINX下載地址:http://nginx.org/en/download.html
- Nginx官網提供了三個類型的版本
- Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
- Stable version:最新穩定版,生產環境上建議使用的版本
- Legacy versions:遺留的老版本的穩定版
wget http://nginx.org/download/nginx-1.4.2.tar.gz
(2)解壓NGINX
tar -zxvf nginx-1.4.2.tar.gz
(3)進入安裝包目錄
cd nginx-1.4.2
(4)編譯安裝 ./configure
make && make install
安裝成功後可以在/usr/local/看到nginx目錄
(5)查看pcre版本
nginx -v
(6)啓動NGINX
./nginx
(7)檢查配置文件nginx.conf的正確性命令
./nginx -t
(8)重啓NGINX
./nginx -s reload
(9)停止NGINX
./nginx -s stop
(10)配置基礎nginx.conf模版
#user nobody;
worker_processes 2; #設置值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日誌位置和日誌級別
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虛擬主機的配置
server
{
listen 80;#監聽端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/nginx/html;#站點目錄
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
(11)啓動完成後,在外網訪問不到,必須關掉防火牆或設置防火牆端口白名單,否則外網不能訪問
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
(12)訪問NGINX歡迎界面,確定順利啓動
總結不好多多擔待,文章只單純個人總結,如不好勿噴,技術有限,有錯漏麻煩指正提出。本人QQ:373965070