When using Linux servers, service startup failures are one of the common issues for new users. Don’t panic! Most startup failures can be resolved through systematic troubleshooting. This article will guide you through simple, step-by-step processes to quickly identify and fix service startup problems.

1. First, Confirm the Service Status: Is the Service Actually Not Running?

Before starting a service, verify its current status. For systems using systemd (common in CentOS 7+/Ubuntu 16.04+), use:

systemctl status 服务名  # Replace with the actual service name, e.g., nginx, mysql
  • Interpretation of Output:
  • active (running): The service is already running; no further troubleshooting needed.
  • inactive (dead): The service is not started; failed: The service failed to start (focus on this case).
  • If failed is shown, proceed to the next step!

2. Check Error Logs: Find “Fault Clues”

System logs record error messages during startup, and logs are the core of troubleshooting. Use these commands to view service startup logs:

# Method 1: View systemd service logs (recommended for beginners)
journalctl -u 服务名 --no-pager  # --no-pager prevents pagination for direct viewing

# Method 2: Check service-specific log files (some services have dedicated logs)
# Example: Nginx error log
cat /var/log/nginx/error.log

Key Log Keywords (focus on these for beginners):
- syntax error: Incorrect configuration file syntax (e.g., typos in Nginx/Apache configs).
- address already in use: Port conflict (e.g., MySQL’s default 3306 is occupied by another program).
- permission denied: Insufficient permissions (e.g., incorrect file permissions for service configs).
- unrecognized service: Service not installed or incorrect service name.

3. Verify Service Installation: Don’t Miss Critical Software!

If a service is not installed, startup will fail with “service not found”. Use these commands to check installation:

# For CentOS/RHEL (using yum/dnf)
yum list installed | grep 服务名  # Example: yum list installed | grep nginx

# For Ubuntu/Debian (using apt)
dpkg -l | grep 服务名  # Example: dpkg -l | grep mysql-server

If not installed: Install the service directly (e.g., for Nginx):

# CentOS/RHEL
sudo yum install nginx -y

# Ubuntu/Debian
sudo apt update && sudo apt install nginx -y

4. Troubleshoot “Configuration File Errors”: Is the Config Correct?

Syntax errors in configuration files are a common cause of startup failures. Take Nginx as an example:
1. Check configuration syntax:

   sudo nginx -t  # Nginx-specific syntax check; use `httpd -t` for Apache

If the output is nginx: configuration file /etc/nginx/nginx.conf test is successful, the config is correct. Otherwise, fix errors based on the message (e.g., nginx: [emerg] bind() to 0.0.0.0:80 failed).

  1. Fix invalid configurations:
    If logs indicate invalid port, open the config file (e.g., /etc/nginx/conf.d/default.conf) and check for port typos (e.g., listen 80808 instead of 8080).

5. Check for “Port Conflicts”: Are Multiple Services Fighting for a Port?

Two services using the same port will cause startup failures (e.g., MySQL default 3306 conflicting with MongoDB).

Troubleshooting:

# Check all processes using a specific port (e.g., port 80)
sudo netstat -tuln | grep 80  # Or: ss -tuln | grep 80

If a conflict is found:
- Terminate the conflicting process (e.g., kill 1234; confirm with ps -ef | grep 进程名 first).
- Or modify the service’s config to use a different port (e.g., change MySQL to 3307 and update client configs).

6. Check “Dependent Services”: Does the Service Depend on Others?

Some services require dependencies to start first (e.g., PostgreSQL needs libpq; Tomcat needs Java).

Troubleshooting:

# View service dependencies (e.g., for Nginx)
systemctl list-dependencies nginx

# Check if dependencies are installed (e.g., Java for Tomcat)
java -version  # Install Java if missing: yum install java-1.8.0-openjdk

Fix: Start dependent services first (e.g., systemctl start mariadb).

7. Check “Permission Issues”: Can the Service Read Config Files?

Services may fail if they lack permissions to access configuration files (e.g., Nginx runs as nobody by default).

Troubleshooting:

# Check config file permissions
ls -l /etc/nginx/nginx.conf

# Fix permissions (for Nginx)
sudo chown -R nobody:nogroup /etc/nginx  # Change owner to the service’s user
sudo chmod -R 644 /etc/nginx/*.conf       # Set read permissions for config files

8. Ultimate Troubleshooting Steps (Follow in Order for Beginners)

  1. Use systemctl status 服务名 to confirm status (mandatory).
  2. Use journalctl -u 服务名 to check logs for error keywords.
  3. If logs show “configuration syntax error”: Fix config files.
  4. If “port in use”: Use netstat to check ports, then change ports or kill processes.
  5. If “service not installed”: Install the service first (yum install or apt install).

Summary

Service startup failures are manageable with systematic troubleshooting. Remember: Check status → View logs → Fix config/port/dependencies. By following these steps, even new users can resolve issues quickly! For complex problems, consult official documentation or communities (e.g., Stack Overflow) and test changes incrementally.

Xiaoye