本教程由阿里雲公開課內容整理。
下載nginx依賴包
開源軟件往往要依賴第三方組件,nginx也不例外。
如果是centos或者redhat發行版本的linux系統,在命令行輸入
#yum -y install openssl openssl-devel pcre pcre-devel
yum
是centos/redhat的軟件安裝命令,-y
選項表示你就不用輸入y
確認了。openssl,openssl-devel這些包具體作用請谷歌。
建立用戶組與用戶
爲什麼要另外創建用戶呢?一般軟件會默認當前登錄用戶爲所有者,這樣不安全,容易被攻破。
#groupadd www
#useradd -g www -s /sbin/nologin www
創建了名爲www的用戶組及用戶,並限制該用戶登錄,只能作爲進程訪問授權
下載nginx
wget下載
wget http://oss.aliyuncs.com/aliyunecs/onekey/nginx/nginx-1.4.4.tar.gz
(這是阿里雲ecs才能用的地址)
或者
wget http://nginx.org/download/nginx-1.8.1.tar.gz
(這個直接從nginx官網下載,版本是1.8.1,自己不喜歡可以換)
解壓
tar -zxvf nginx-1.4.4.tar.gz
tar是解壓命令
然後到解壓之後的目錄下
cd nginx-1.4.4
cd命令詳解
編譯安裝nginx
先想好要安裝在哪裏,不如專門搞一個目錄放這些建站環境軟件
mkdir -p /usr/local/server
mkdir命令詳解
然後跳轉到剛纔目錄cd -
編譯前配置
./configure --user=www --group=www --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
./configure
是在安裝前對軟件進行配置,–user用戶 –group用戶組 –prefix安裝位置,–with啓用模塊
編譯
make
出現錯誤的話很可能是你沒安裝gcc
yum -y install gcc
安裝gcc之後再make
編譯
安裝
make install
安裝完畢!
配置nginx
設置日誌目錄的訪問權限
linux中權限十分重要,在這裏先不展開
chmod 775 /usr/local/server/nginx/logs
chmod
是文件改變權限
chown -R www:www /usr/local/server/nginx/logs
chown
是改變所有者
設置 Web 程序目錄的訪問權限
#mkdir /usr/local/server/web
#chmod 775 /usr/local/server/web
#chown -R www:www /usr/local/server/web
設置 nginx 程序執行權限
#chmod 775 /usr/local/server/nginx/sbin/nginx
創建 nginx 服務
vim /etc/init.d/nginx
vim
是linux中文件修改命令,文件不存在時自動創建。
/etc/init.d 目錄通常是放置服務程序的目錄,服務程序的
作用是可以在計算機啓動時自動啓動。
按一下i鍵粘貼下面內容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
nginxd=/usr/local/server/nginx/sbin/nginx nginx_config=/usr/local/server/nginx/conf/nginx.conf nginx_pid=/usr/local/server/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ] && netstat -tunpl | grep nginx &> /dev/null;then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog!"
$nginxd -c ${nginx_config} RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog!"
$nginxd -s stop RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/nginx
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog!"
#kill -HUP `cat ${nginx_pid}`
$nginxd -s reload RETVAL=$?
echo
}
# See how we were called. case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|help}"
exit 1
esac
exit $RETVAL
這是shell腳本
按esc,輸入:wq
退出
然後爲該文件設置權限
chmod 775 /etc/init.d/nginx
接下來工作需要先等php安裝完畢。