centos 7 安裝 nginx-1.11.10(騰訊雲)
在centos 7 下安裝 nginx-1.11 前需要先切換到root環境,通過命令 su root 切換,然後再輸入密碼, 如果不能切換需要把下載的nginx文件夾給予777的權限
bash
#su root
密碼輸入
下載nginx-1.11.10的壓縮包文件到根目錄,官網下載地址:nginx.org/download/nginx-1.11.10.tar.gz
#yum update #wget nginx.org/download/nginx-1.11.10.tar.gz
解壓tar.gz壓縮包文件,進去nginx-1.11.10
#tar -xzvf nginx-1.11.10.tar.gz #cd nginx-1.11.10
進去後如果發現有configure這個文件,說明這個源碼包安裝前需要先進行配置,主要是為了檢查當前的環境是否滿足要安裝軟件的依賴關系,如果沒有這個文件說明是二進制包,解壓後直接使用不用configure
#./configure
通過安裝前的配置檢查,發現有報錯 檢查中發現一些依賴庫沒有找到,這時候需要先安裝nginx的一些依賴庫
#yum -y install pcre* #yum -y install gcc-c++ #yum -y install zlib* #yum -y install openssl
再次進行檢查操作 ./configure 沒發現報錯顯示,接下來進行編譯並安裝的操作
#make && make install
查看nginx安裝後在的目錄,可以看到已經安裝到 /usr/local/nginx 目錄了
#whereis nginx $nginx: /usr/local/nginx
啟動nginx服務
#cd /usr/local/nginx/sbin/
#./nginx
nginx默認使用80端口,若啟動報端口錯誤,則kill80端口的進程,或者nginx配置使用其他端口
通過ping localhost 或在瀏覽器直接訪問,ping成功或出現下面頁面則安裝成功。
命令:curl localhost/index.html 如果返回html代碼也代表安裝成功。
[root@VM_0_14_centos sbin]# curl localhost/index.html
安裝好後,當系統重啟時每次都要去手動啟動nginx服務,所以需要把nginx加入開機自啟動,參考nginx設置開機啟動:http://zixuephp.net/article-206.html
創建開機啟動命令腳本文件:
#vi /etc/init.d/nginx
在這個nginx文件中插入一下啟動腳本代碼,啟動腳本代碼來源網絡復制
#! /bin/bash # chkconfig: - 85 15 PATH=/usr/local/nginx DESC="nginx daemon" NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/logs/$NAME.pid scriptNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { $DAEMON -s stop || echo -n "nginx not running" } do_reload() { $DAEMON -s reload || echo -n "nginx can‘t reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0
設置所有人都有對這個啟動腳本nginx文件的執行權限
#chmod a+x /etc/init.d/nginx
把nginx加入系統服務中
#chkconfig --add nginx
把服務設置為開機啟動
#chkconfig nginx on
reboot重啟系統生效
#啟動nginx服務
#systemctl start nginx.service
#停止nginx服務
#systemctl stop nginx.service
#重啟nginx服務
#同時也可以通過下面的命令進行服務重啟,停止操作
#service nginx restart
#service nginx start
#service nginx stop
如果服務啟動的時候出現 Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
nginx not running 的錯誤,通過nginx -c 參數指定配置文件即可解決
#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果服務啟動中出現 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的錯誤,可以先通過service nginx stop 停止服務,再啟動就好。
centos 7 安裝 nginx-1.11.10(騰訊雲)