http://www.cnblogs.com/vicowong/p/4158113.html更換了部分包位置 抑党,僅記錄
安裝步驟:
1.準備
1.1 顯示系統(tǒng)版本
[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@centos ~]# uname -a
Linux tCentos7 3.10.0-123.13.1.el7.x86_64 #1 SMP Tue Dec 9 23:06:09 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
1.2 安裝基本軟件包
[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y
[root@centos ~]# yum install net-tools bind-utils -y
1.3 顯示IP地址 (centos7需要先安裝 net-tools bind-utils包)
[root@centos ~]# ifconfig|grep inet
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
2.編譯安裝nginx
2.1 下載包
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
[root@centos ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz
[root@centos ~]# wget http://zlib.net/zlib-1.2.11.tar.gz
[root@centos ~]# wget http://www.openssl.org/source/openssl-1.0.2l.tar.gz
[root@centos ~]# wget https://github.com/jemalloc/jemalloc/releases/download/4.2.1/jemalloc-4.2.1.tar.bz2
2.2 安裝依賴
[root@centos ~]# yum install zlib-devel openssl-devel -y
2.3 關(guān)閉selinux (不關(guān)有時會添加不了用戶,或者重啟后沒法開機)
[root@centos ~]#??vim /etc/selinux/config
屏蔽以下兩行
#SELINUX=enforcing
#SELINUXTYPE=targeted
添加以下一行
SELINUXTYPE=disabled
保存姓言,退出
重啟后,查詢是否關(guān)閉(顯示Disabled則表示關(guān)閉)
[root@centos ~]# ?shutdown -r now
[root@centos ~]# ?getenforce
Disabled
2.4 安裝Pcre
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf pcre-8.41.tar.gz
[root@centos ~]# cd pcre-8.41
[root@centos ~]# ./configure
[root@centos ~]# make && make install
2.5 安裝openssl
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf openssl-1.0.2l.tar.gz
[root@centos ~]# cd openssl-1.0.2l
[root@centos ~]# ./config
[root@centos ~]# make && make install
2.6 安裝zlib
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf zlib-1.2.11.tar.gz
[root@centos ~]# cd zlib-1.2.11
[root@centos ~]# ./configure
[root@centos ~]# make && make install
2.7 安裝jemalloc
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar xjf jemalloc-4.2.1.tar.bz2
[root@centos ~]# cd jemalloc-4.2.1
[root@centos ~]# ./configure
[root@centos ~]# make && make install
[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
[root@centos ~]# ldconfig
2.3 創(chuàng)建www用戶和組畔裕,創(chuàng)建www虛擬主機使用的目錄秽五,以及Nginx使用的日志目錄,并且賦予他們適當?shù)臋?quán)限
[root@centos ~]# groupadd www
[root@centos ~]# useradd -g www www -s /sbin/nologin
[root@centos ~]# mkdir -p /data/www
[root@centos ~]# chmod +w /data/www
[root@centos ~]# chown -R www:www /data/www
***如果沒法創(chuàng)建用戶瞬痘,需要檢查SELinux狀態(tài)是否關(guān)閉
2.8 安裝nginx
[root@centos ~]# cd /usr/local/src/
[root@centos ~]# tar zvxf nginx-1.6.2.tar.gz
[root@centos ~]# cd nginx-1.6.2
[root@centos ~]# ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/download/openssl-1.0.2l --with-zlib=/usr/local/download/zlib-1.2.11 --with-pcre=/usr/local/download/pcre-8.41 --with-ld-opt="-ljemalloc"
[root@centos ~]# make && make install
2.9 修改 nginx.conf
[root@centos ~]# vim /opt/nginx/conf/nginx.conf
修改前面幾行為:
user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
events{
use epoll;
worker_connections 65535;
}
找到故慈,并修改 root 行的內(nèi)容
location / {
root /data/www;
index index.html index.htm;
}
保存,退出
2.10 建立測試首頁
[root@centos ~]# vim /data/www/index.html
nginx index.html
index.html
保存框全,退出
2.11 測試和運行
[root@centos ~]# cd /opt/nginx
[root@centos ~]# ldconfig
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf -t
如果顯示下面信息闽寡,即表示配置沒問題
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
查看jemalloc是否生效喳张,需要先啟動nginx
[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf
[root@centos ~]# lsof -n | grep jemalloc
ginx 2346 root mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2347 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2348 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2349 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2350 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
2.12 防火墻添加80端口
[root@centos ~]# iptables -L|grep ACCEPT
[root@centos ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@centos ~]# firewall-cmd --reload
[root@centos ~]# iptables -L|grep ACCEPT
2.13 瀏覽器打開
http://192.168.1.10
顯示出歡迎內(nèi)容贷揽,則表示成功
2.14 作為服務(wù)施无,開機后啟動
[root@centos ~]# vim /usr/lib/systemd/system/nginx.service
增加以下內(nèi)容
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -t
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
:wq 保存退出
[root@centos ~]# systemctl enable nginx.service
[root@centos ~]# systemctl list-unit-files|grep enabled|grep nginx
2.15 啟動服務(wù)
[root@centos ~]#?./sbin/nginx -s stop
[root@centos ~]# systemctl daemon-reload
[root@centos ~]# systemctl start nginx.service
[root@centos ~]# systemctl status nginx.service -l
[root@centos ~]# ps -ef|grep nginx
[root@centos ~]# lsof -n | grep jemalloc