1. Install Nginx and its dependencies

The first is the old way of using SSH to connect to the server, remember the old code?

SSH -t username @Server IP address or domain name -p 22 <! SSH -t [email protected] -p 22 SSH -t [email protected] -p 22Copy the code

Enter the above command in the terminal and press Enter to ask us to enter the password. This password is not visible, so make sure you enter it correctly.

After connecting to the server, we switch to the usual installation path, which of course is /usr/src on my server, and start on the terminal:

<! -- Go to the installation directory -->cd/usr/src <! Create Nginx folder to store Nginx related resources and dependencies --> mkdir Nginx <! Yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel <! It doesn't matter that you don't need to install anything, we will then reinstall the specified version --> <! - download pcre wget -- > ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz <! Tar -zxvf pcl-8.40.tar. gz <! -- Change to pcre directory -->cdPcre 8.40 <! /configure <! Make <! --> make install <! Nginx home directory -->cd. <! Zlib --> wget http://zlib.net/zlib-1.2.11.tar.gz <! Tar -zxvf zlib-1.2.11.tar.gz <! -- Go to zlib -->cdZlib - 1.2.11 <! /configure make make install <! Nginx home directory -->cd. <! Download and prepare SSL --> wget http://www.openssl.org/source/ openssl - fips - 2.0.14. Tar. Gz <! Tar -zxvf openssl-fips-2.0.14.tar.gz <! Yum -y install openssl openssl-devel <! - download and install nginx wget -- > http://nginx.org/download/nginx-1.4.2.tar.gz tar - ZXVF nginx - 1.4.2. Tar. GzcdNginx - 1.4.2 <! Nginx installation directory /opt/ Nginx /configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre make make installCopy the code

At this point, our Nginx installation is complete, but we still need to do more to configure the server, add SSL access, set up the service, and boot up

Configuring the Server

On the Internet about the server set up a lot, but the accurate description is not so much, and I just looked at their things on the ha Ha. The correct configuration method is as follows:

<! -- Go to nginx Settings directory -->cd/opt/nginx/conf <! --vim edit nginx configuration file --> vi nginx.confCopy the code

My nginx.conf is as follows:

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;
# Note that this is related to setting up the machine and is not recommended to change
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log logs/host.access.log main;

        location / {
        root   html;
                index  index.html index.htm;
        # proxy_pass http://localhost; 
        # proxy_set_header Host $host;
        # proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        # proxy_pass http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        # root html;
        # fastcgi_pass 127.0.0.1:9000;
        # fastcgi_index index.php;
        # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
        # include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        # deny all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    # listen 8000;
    # listen somename:8080;
    # server_name somename alias another.alias;

    # location / {
    # root html;
    # index index.html index.htm;
    #}
    #}

You must set this to correct when accessing HTTPS
    # HTTPS server
    #
    server {
        listen       443;
        server_name  localhost acheng1314.cn www.acheng1314.cn;

        ssl                  on;
        Here is the signature you requested, drop it in the cert directory under confssl_certificate cert/214217283570796.pem; ssl_certificate_key cert/214217283570796.key; ssl_session_timeout 5m; Ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:! NULL:! aNULL:! MD5:! ADH:! RC4; ssl_prefer_server_ciphers on; location / {# root html;
        # index index.html index.htm;
    proxy_pass http://localhost;
    proxy_set_header   Host    $host;
    proxy_set_header   X-Real-IP   $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; }}Port 8080 on the local machine
server {  
    listen       80;  
    server_name  *.acheng1314.cn acheng1314.cn;  
    location / {  
        proxy_pass http://localhost:8080/;  
        proxy_set_header   Host    $host;  
        proxy_set_header   X-Real-IP   $remote_addr;  
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; }}}Copy the code

In fact, when writing this, it is important to note that no matter what application port does not conflict! For example, my nginx is bound to port 80. If Tomcat sets port 80, my Settings will fail to be forwarded even if they are bound to localhost. After all, network ports can only be occupied by one application.

<! /opt/nginx/sbin/nginx -t <! /opt/nginx/sbin/nginx <! /opt/nginx/sbin/nginx -t <! - Of course, by the time we get here, we will not be able to access, after all, our firewall is still blocking port 443, so continue to walk. -- > <! --> /sbin/iptables -i INPUT -p TCP --dport 443 -j ACCEPT <! - save the firewall configuration - > / etc/rc. D/init. D/iptables save <! -- If the configuration file takes effect --> /etc/init.d/iptables statusCopy the code

At this point, we can test the server, according to the normal speaking, my current server is HTTP and HTTPS are fully supported.

3. Set up the service and boot

In fact, there is basically nothing to notice here, as long as the nginx path is set correctly.

#! /bin/sh
# Name:nginx4comex
# nginx - this script starts and stops the nginx daemon
#
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /comexHome/nginx/nginx.pid
#
# Created By http://comexchan.cnblogs.com/

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"
prog=$(basename $NGINX_DAEMON_PATH)

start() {
    [ -x $NGINX_DAEMON_PATH] | |exit 5
    [ -f $NGINX_CONF_FILE] | |exit 6
    echo -n $"Starting $prog:"
    daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE
    return $retval
}

stop() {
    echo -n $"Stopping $prog:"
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog:"
    killproc $NGINX_DAEMON_PATH -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $NGINX_DAEMON_PATH -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "The $1" in
    start)
        rh_status_q && exit 0
        The $1
        ;;
    stop)
        rh_status_q || exit 0
        The $1
        ;;
    restart|configtest)
        The $1
        ;;
    reload)
        rh_status_q || exit 7
        The $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit0;; *)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esacCopy the code

The above code is the code to create the service and save it in an nginx4comex file (which I wrote using Vim again in /opt/nginx). Note that the following code corresponds to your configuration.

NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"Copy the code

Then we continue with terminal instruction.

<! Chmod u+x nginx4comex <! --> cp nginx4comex /etc/init.d <! Service nginx4comex status <! Vim /etc/rc.local <! /etc/init.d/nginx4comex start <! -- Now that we have added the boot, let's check the effect --> rebootCopy the code

Finally, we have set up the Nginx agent Tomcat, and also set up the corresponding server application self-start.

Attention! Nginx4comex will not be enabled by chkconfig. I don’t know why, but the author’s article does use chkconfig to boot nginx4comex. If you are interested in Linux, try it out.


If you recognize what I have done and think it is of some help to you, I hope you can also give me a cup of coffee, thank you.

Alipay Donation