Install nginx in linux environment (and deploy static files)

Install nginx in linux environment

1. The environment required to install nginx

1.1 Install gcc
command: yum install gcc-c++
(pop up option, enter y)
1.2 Install PCRE
command: yum install -y pcre pcre-devel
1.3 Install zlib
command: yum install -y zlib zlib-devel
1.4 Install openssl
command: yum install -y openssl openssl-devel

2. Compile and install

2.1 Upload the compressed package to the specified directory

2.2 Decompress the nginx compressed package: tar -zxvf nginx-1.8.0.tar.gz

2.3 Query configuration parameters, the meaning of each parameter, not listed here: ./configure --help

2.4 Parameter settings (for reference only, the path configuration can be different, all belong to the load, installation path, can be used directly):
./configure
–prefix=/usr/local/nginx
–pid-path=/var/run/nginx/nginx .pid
–lock-path=/var/lock/nginx.lock –error
-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
– with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
–http-fastcgi-temp-path=/var /temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/scgi
Note: Set the temporary file path to :/Var/temp/nginx/client, you need to manually create a new temp under the var directory, a new nginx directory under temp, and the client below.

2.5 Compile and install: make install (the command here is directly executed in the decompressed nginx-1.8.0 directory, nginx is installed in: /usr/local/nginx)

2.6 View the installation path
cd /usr/local/nginx
There are three folders: conf, sbin, html
conf is the configuration file, mainly nginx.conf
sbin is the nginx file.

2.7 启动nginx:
cd /usr/local/nginx/sbin
./nginx

2.8 Query nginx entry: ps aux|grep nginx
followed by ./nginx is the main process and the
other is the worker process

2.8 Stop nginx
Method 1: ./nginx -s stop ()
This method is equivalent to first finding out the nginx process id and then using the kill command to forcibly kill the process.
Method 2: ./nginx -s quit
This method of stopping step is to stop when the nginx process finishes processing tasks.

2.9 Restart nginx
./nginx -s reload is
used to reload the configuration file after changing nginx.conf to make the configuration file effective.

2.10 Whether the test is successful (ip+80 default port)
Insert picture description here

3. Start nginx at boot

3.1 vi /etc/init.d/nginx enter the following content
Function: You can directly use the script here to operate ng, such as
viewing status: /etc/init.d/nginx status
Start: /etc/init.d/nginx start
Stop: /etc/init.d/nginx stop
Restart: /etc/init.d/nginx restart

#!/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_config=/usr/local/nginx/conf/nginx.conf
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

3.2 Set this file so that all users can access
chown a+x /etc/init.d/nginx
(a means all, x means execute)
3.3 Configure to restart and
open the rc.local file: vi /etc/rc.
Add at the bottom of local : /etc/init.d/nginx start

4. Deploy a static file

4.1 Open the configuration file under the installation directory (here: /usr/loacal/nginx/conf/nginx.conf)
vi nginx.conf
configure the path and page in the location, as shown in the figure below:
Insert picture description here
In addition, it is best to set the default in the server If port 80 is changed, port 80 is prone to port conflict, here is changed to 8081, as shown in Figure
Insert picture description here
4.2 ifconfig view port, ip+port access test, and the page is successful.

Insert picture description here

If there is something wrong, please testify.

Record the problem of deploying nginx static page on Alibaba Cloud server

1. The ip can be pinged, but the nginx page deployed by the server cannot be accessed through the ip and port
. Cause: If the Alibaba Cloud server is deployed with centos 7 and systems after 7, 80 is occupied by the firewall (the default is not enabled before 7), and the second is required Configure a security group for the server. This security group is equivalent to a firewall. Commonly used ports of various software need to be released. For example, network access ports 80 and 443 need to be configured with security rules.
2. After the security group configuration is completed, access to the ng configuration page shows 403. Here is because the two processes of ng are a master and a worker (master is used to manage the workers), and the startup users are different. You need to find your own ng configuration file : Nginx/conf/nginx.conf Change the "user nobody;" in the first line of the file to the normal user of ng. Note that this line of code is generally commented out, and the # sign needs to be deleted.

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/109278338
Recommended