centos Builds nginx Picture Server
Published: 2019-05-27

Build Nginx Picture Server


1. nginx Installation Environment

nginx is developed in C language and is recommended to run on linux. Centos6.5 is used as the installation environment for this tutorial.


To install nginx, you need to compile the source code downloaded from the official website. The compilation depends on the gcc environment. If there is no gcc environment, you need to install GCC: Yum Install GCC-C


PCRE (perl Compatible Regular Expressions) is a Perl library, including Perl compatible regular expression libraries.Nginx's http module uses pcre to parse regular expressions, so pcre libraries need to be installed on linux.

yum install -y pcre pcre-devel

Note: pcre-devel is a secondary development library developed by pcre.Nginx also needs this library.

· zlib

zlib library provides many compression and decompression methods. nginx uses zlib to gzip the contents of http packets, so zlib library needs to be installed on linux.

yum install -y zlib zlib-devel

· openssl

OpenSSL is a powerful secure socket layer password library, which includes main password algorithms, commonly used key and certificate encapsulation management functions and SSL protocol, and provides rich application programs for testing or other purposes.

nginx not only supports http protocol, but also supports https (that is, http is transmitted over ssl protocol), so openssl library needs to be installed on linux.

yum install -y openssl openssl-devel

1. Compile and install

Copy nginx-1.8.0.tar.gz to linux Server.


tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

1. configure

./configure --help queries detailed parameters (refer to the appendix of this tutorial: nginx compilation parameters)

parameter settings are as follows:

./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 \


Note: The temporary file directory is specified as /var/temp/nginx above. Temp and nginx directories need to be created under /var

1. Compile and install


make  install

Installation Successfully View Installation Directory:


1. Start nginx

cd /usr/local/nginx/sbin/


Query nginx Process:


15098 is the process id of the main process in nginx and 15099 is the process id of the working process in nginx

Note: Execute. /nginx starts nginx, where -c can specify the loaded nginx configuration file as follows:

./nginx -c /usr/local/nginx/conf/nginx.conf

If -c is not specified, nginx will load the conf/nginx.conf file by default at startup, and the address of this file can also be specified when compiling and installing nginx. /configure parameter (--conf-path= = pointing to configuration file (Nginx. conf))

1. Stop nginx

Mode 1, Fast Stop:

cd /usr/local/nginx/sbin

./nginx -s stop

This method is equivalent to finding out the id of the nginx process before using the kill command to force the process to be killed.

mode 2, complete stop (recommended):

cd /usr/local/nginx/sbin

./nginx -s quit

The stop step in this way is to stop after the nginx process finishes processing the task.

1. Restart nginx

Mode 1, stop before starting (recommended):

Restarting nginx is equivalent to stopping nginx before starting nginx, that is, executing the stop command before executing the start command.

as follows:

./nginx -s quit


Mode 2, Reload Configuration File:

When nginx's configuration file nginx.conf is modified, nginx needs to be restarted for the configuration to take effect, and the configuration information can take effect in nginx using -s reload without stopping nginx before starting nginx, as follows:

./nginx -s reload

1. Testing

nginx installation is successful, start the nginx, you can access the nginx on the virtual machine:


This shows that the installation on nginx was successful.

1. Start-up nginx

1. Script shell

This is handled by writing shell scripts

vi/etc/init.d/Nginx (enter the following code)




# 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






# 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


   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}



   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL


# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd



        [ $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




# See how we were called.

case "$1" in















        status $prog




        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1


exit $RETVAL



:wq  Save and Exit

1. Set file access rights

chmod a+x/etc/init.d/Nginx (a+x = = > allusercanexecute executable executable by all users)

/etc/init.d/nginx status

This makes it easy to operate nginx on the console: check the current status of Nginx, start Nginx, stop Nginx, restart Nginx ...


If nginx's configuration file nginx.conf is modified, the above command can also be used to reload the new configuration file and run it. This command can be added to the rc.local file, so that nginx will be started by default when starting up.

1. Add to rc.local file

vi /etc/rc.local

Add a line/etc/init.d/Nginx Start to save and exit, and the next restart will take effect.



Open Port


/sbin/iptables-input-tcp-dport 80-jaccept opens Linux's external access port 3306

/etc/rc.d/init.d/iptables save-permanently saves the changes to the Firewall



1. Effect

For example, pictures are uploaded to the /home/ftpuser/www/images directory through ftp service. I want to access the picture files in the ftp directory through accessing the Nginx server, and the url is is, using http requests to access resource files that would otherwise require ftp requests.(If you don't understand, you can see the final graphical test first)

2. Implementation

① An images folder needs to be created under nginx/html (this is not the path actually accessed)

mkdir /usr/local/nginx/html/images


② Modify nginx/conf/nginx.conf Add Another location to the Default server inside and Specify the Actual Path:

location /images/ {

root /home/ftpuser/www/;

autoindex on;



nginx needs to be restarted after modification

./nginx -s reload


1)root maps images to /home/ftpuser/www/images/

2)autoindex on turns on browsing.


1)ftpuser needs to be created by itself. The ftp component installed in Linux (8 steps completed) has already been created, so it will not be created here. If not, it needs to be created by itself, with any name.

2)root /home/ftpuser/www/ this is followed by no images and a space after root.There is an images directory under the www directory for storing pictures!!

③ Modify User Access Rights

chown ftpuser /home/ftpuser

chmod 777 -R /home/ftpuser