yum install -y nginx
通過yum安裝的時候提示下面的錯誤
[root@localhost yum.repos.d]# yum install nginx 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile 沒有可用軟件包 nginx。 錯誤:無須任何處理
需要添加nginx的源
[root@localhost yum.repos.d]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
該命令執行之后,會在/etc/yum.respos.d下面多出一個nginx.repo
[root@localhost yum.repos.d]# ll 總用量 40 -rw-r--r--. 1 root root 1572 12月 1 2016 CentOS-Base.repo -rw-r--r--. 1 root root 1572 12月 1 2016 CentOS-Base.repo.backup -rw-r--r--. 1 root root 1664 10月 24 10:36 CentOS-Base.repo.bak -rw-r--r--. 1 root root 1309 8月 30 2017 CentOS-CR.repo -rw-r--r--. 1 root root 649 8月 30 2017 CentOS-Debuginfo.repo -rw-r--r--. 1 root root 314 8月 30 2017 CentOS-fasttrack.repo -rw-r--r--. 1 root root 630 8月 30 2017 CentOS-Media.repo -rw-r--r--. 1 root root 1331 8月 30 2017 CentOS-Sources.repo -rw-r--r--. 1 root root 3830 8月 30 2017 CentOS-Vault.repo -rw-r--r--. 1 root root 113 7月 15 2014 nginx.repo
然后再執行安裝命令
yum install -y nginx
安裝之后,可以查看nginx的默認安裝目錄
[root@localhost yum.repos.d]# whereis nginx nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz [root@localhost yum.repos.d]# pwd /etc/yum.repos.d
以下是Nginx的默認路徑:
(1) Nginx配置路徑:/etc/nginx/
(2) PID目錄:/var/run/nginx.pid
(3) 錯誤日志:/var/log/nginx/error.log
(4) 訪問日志:/var/log/nginx/access.log
(5) 默認站點目錄:/usr/share/nginx/html
事實上,只需知道Nginx配置路徑,其他路徑均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查詢到
nginx相關的驗證命令及啟動命令
[root@localhost yum.repos.d]# nginx [root@localhost yum.repos.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost yum.repos.d]# nginx -s reload
nginx 啟動
nginx -t 測試命令
nginx -s relaod 修改nginx.conf之后,可以重載
有些源經常失效,下面放上最新鏈接 直接去里面下載 rpm -Uvh 下載的rpm 就好了
http://nginx.org/packages/centos/7/noarch/RPMS/(源鏈接)
最新安裝 rpm -Uvh
http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
配置開機啟動
進入到/etc/init.d/目錄下,創建文件nginx
cd /etc/init.d/
vim nginx
nginx文件具體配置信息如下:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx # 你的nginx真實啟動文件路徑 nginx_config=/usr/local/nginx/conf/nginx.conf # nginx相關配置文件路徑 nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL 保存nginx文件並賦權 chmod 755 nginx 將nginx權限設置為自己可以read、write、exec,其他用戶只能有read、exec權限,沒有write權限 為nginx加上service相關命令權限 chkconfig --add nginx chkconfig nginx on 開啟nginx的service命令
校驗nginx的service命令是否成功
service nginx start 執行不報錯表示nginx已經啟動
重啟centos服務器再次驗證是否nginx已經啟動
重啟之前service nginx stop停止nginx服務,之后執行reboot,開機之后執行ps aux | grep nginx如果后台顯示nginx已經啟動,那么表示nginx的安裝和開機自啟動已經成功配置 本文章旨在記錄自己的平時學習過程,有不正確的和值得改進的真誠希望園友提出意見或建議,一起學習一起進步。
http://www.cnblogs.com/attitudeJinChi/p/9347120.html 附上原鏈接
