centos 7.0 systemctl安装nginx 自启动

nginx 在centos 7.0下 ,启动文件nginx.service 文件存放位置  

/usr/lib/systemd/system 和user目录

/etc/systemed 

在centos 7下想要自启动nginx服务,

systemctl 服务 启动的脚本文件都放在 /usr/lib/systemd/systemuser目录下如下图

进入system目录 把nginx.service放到里面,具体写法可以参考目录下其他的.service写法

 nginx.service 内容参考如下

代码如下

[Unit]
Description=nginx1.9.10
After=network.target

[Service]
Type=forking
PIDFile=/usr/etc/nginx1910/logs/nginx.pid
ExecStartPre=/usr/etc/nginx1910/sbin/nginx -t -c /usr/etc/nginx1910/conf/nginx.conf
ExecStart=/usr/etc/nginx1910/sbin/nginx
ExecReload=/usr/etc/nginx1910/sbin/nginx
ExecStop=/usr/etc/nginx1910/sbin/nginx
PrivateTmp=true


[Install]
WantedBy=multi-user.target

 我是直接下载文件到windows桌面,直接修改在上传的,上传完nginx.service后,要执行下systemctl daemon-reload ,然后再执行systemctl start nginx.service ,启动后查看下信息,systemctl status nginx查看开机启动是否有问题 

然后再测试stop命名是否可用,systemctl stop nginx 测试没有问题

 

测试启动nginx ,systemctl start nginx  没有问题 

设置开机启动

systemctl enable nginx

测试是否开机可用,立即重启centos 7 , 重启命令:shutdown -r now

 

测试没有问题,依旧可以访问 到此完成,

 

 *********************************************************************************************

1.查看systemctl 所有启动服务    centos 7.0

systemctl list-units --type=service

[root@iZwz9atg4jn10qwmnrey29Z system]# systemctl list-units --type=service

实例

[root@iZwz9atg4jn10qwmnrey29Z system]# systemctl list-units --type=service
  UNIT                                   LOAD      ACTIVE SUB     DESCRIPTION
  aegis.service                          loaded    active running LSB: aegis update.
  agentwatch.service                     loaded    active exited  SYSV: Starts and stops guest agent
  aliyun-util.service                    loaded    active exited  Initial Aliyun Jobs
  aliyun.service                         loaded    active running Aliyun Service Daemon
  cloud-config.service                   loaded    active exited  Apply the settings specified in cloud-config
  cloud-final.service                    loaded    active exited  Execute cloud user/final scripts
  cloud-init-local.service               loaded    active exited  Initial cloud-init job (pre-networking)
● cloud-init-upgrade.service             loaded    failed failed  cloud-init upgrade service
  cloud-init.service                     loaded    active exited  Initial cloud-init job (metadata service crawler)
  crond.service                          loaded    active running Command Scheduler
  dbus.service                           loaded    active running D-Bus System Message Bus
  getty@tty1.service                     loaded    active running Getty on tty1
  httpd.service                          loaded    active running SYSV: Apache is a World Wide Web server.  It is used to serve HTML files and CGI.
  iptables.service                       loaded    active exited  IPv4 firewall with iptables
  kmod-static-nodes.service              loaded    active exited  Create list of required static device nodes for the current kernel
● ldconfig.service                       not-found active exited  ldconfig.service
  memcached.service                      loaded    active running SYSV: Startup script for the Memcache
  mysqld.service                         loaded    active exited  LSB: start and stop MySQL
  network.service                        loaded    active running LSB: Bring up/down networking
  nginxd.service                         loaded    active running SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy serve
  ntpd.service                           loaded    active running Network Time Service
  polkit.service                         loaded    active running Authorization Manager
  pureftpd.service                       loaded    active running SYSV: Pure-FTPd is an FTP server daemon based upon Troll-FTPd
  rhel-autorelabel-mark.service          loaded    active exited  Mark the need to relabel after reboot
  rhel-dmesg.service                     loaded    active exited  Dump dmesg to /var/log/dmesg
  rhel-import-state.service              loaded    active exited  Import network configuration from initramfs
  rhel-readonly.service                  loaded    active exited  Configure read-only root support
  rsyslog.service                        loaded    active running System Logging Service
  sshd-keygen.service                    loaded    active exited  OpenSSH Server Key Generation
  sshd.service                           loaded    active running OpenSSH server daemon
  systemd-fsck-root.service              loaded    active exited  File System Check on Root Device
  systemd-hwdb-update.service            loaded    active exited  Rebuild Hardware Database
  systemd-journal-catalog-update.service loaded    active exited  Rebuild Journal Catalog
  systemd-journal-flush.service          loaded    active exited  Flush Journal to Persistent Storage
  systemd-journald.service               loaded    active running Journal Service
  systemd-logind.service                 loaded    active running Login Service
  systemd-random-seed.service            loaded    active exited  Load/Save Random Seed
  systemd-remount-fs.service             loaded    active exited  Remount Root and Kernel File Systems
  systemd-sysctl.service                 loaded    active exited  Apply Kernel Variables
  systemd-tmpfiles-setup-dev.service     loaded    active exited  Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service         loaded    active exited  Create Volatile Files and Directories
  systemd-udev-trigger.service           loaded    active exited  udev Coldplug all Devices
  systemd-udevd.service                  loaded    active running udev Kernel Device Manager
  systemd-update-done.service            loaded    active exited  Update is Completed
  systemd-update-utmp.service            loaded    active exited  Update UTMP about System Boot/Shutdown
  systemd-user-sessions.service          loaded    active exited  Permit User Sessions
  systemd-vconsole-setup.service         loaded    active exited  Setup Virtual Console
  wdcp.service                           loaded    active running SYSV: wdCP system

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

48 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

  

 

2.systemctl启动某一个服务

测试nginx服务是否可以启动  systemctl start nginx(这里是服务名)

获取详细的报错信息 就用systemctl status nginx.service 或者journalctl -xn 如下图

 

3.systemctl查看某个服务状态 

测试nginx服务状态是否可用 systemctl status nginx(nginx或者nginx.service) 这个名称写得是服务名或者文件名

 实例:

 

 

参考资料:

http://www.linuxidc.com/Linux/2014-07/104487.htm

systemctl 命令完全指南 ★★★★★

centos7下使用yum安装mysql并创建用户,数据库以及设置远程访问 

 

posted @ 2016-09-07 10:53  星耀学园  阅读(1376)  评论(0)    收藏  举报