Linux under source installation and configuration Nginx

Source: Internet
Author: User
Tags openssl nginx server

Experimental environment
    1. A minimal installation of the CentOS 7.3 virtual machine
Installing Nginx

Installing Nginx Dependency Packages

yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim

Nginx relies on Pcre, zlib, OpenSSL, if there is a problem when configuring before compiling
Three packages can be installed using Yum (Pcre-devel, Zlib-devel, Openssl-devel)

Download Nginx Source package from Nginx official website

wget http://nginx.org/download/nginx-1.12.2.tar.gz

Extract the Nginx source package to /root/nginx , and view Nginx file structure

tar -xzvf nginx-1.12.2.tar.gz

/root/nginxConfiguring pre-compilation in a directory

cd /root/nginx*./configure --prefix=/usr/local/nginx --with-http_ssl_module

To /root/nginx perform a compilation installation in a directory

make && make install

Start Nginx

Shutting down the firewall

setenforce 0systemctl stop firewalldsystemctl disable firewalld

Go to the installation directory to /usr/local/nginx view the directory structure

cd /usr/local/nginxpwdls

Start Nginx

/usr/local/nginx/sbin/nginx

To see if the Nginx process is started

ps aux | grep nginx

View the port numbers that Nginx occupies

netstat -tlnp

Use a local host to access the Nginx server on the virtual machine

Stop Nginx

Three ways to stop Nginx

# 1. 立即停止Nginx服务/usr/local/nginx/sbin/nginx -s stop# 2.完成当前任务后停止/usr/local/nginx/sbin/nginx -s quit# 3.杀死Nginx进程killall nginx
Add the Nginx command to the environment variable

Use soft links to nginx link to/usr/local/sbin

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbinll /usr/local/sbin/ | grep "nginx"

Show Current environment variablesPATH

echo $PATH

Edit .bash_profile File

vim ~/.bash_profile

Add the following to the end of the .bash_profile file

export PATH=$PATH:/usr/local/nginx/sbin

Reference .bash_profile File

source ~/.bash_profile

Using nginx commands

# 启动nginxnginx# 停止nginxnginx -s quit
Put nginxCommand added to system service

Create and edit files/root/service-nginx.sh

#!/bin/sh## filename:service-nginx.sh## nginx-this script starts and stops the Nginx daemon## chkconfig:-15# des Cription:nginx is an HTTP (s) server, HTTP (s) reverse # Proxy and IMAP/POP3 proxy server# Processname:ngin  x# config:/etc/nginx/nginx.conf# config:/etc/sysconfig/nginx# pidfile:/var/run/nginx.pid# Source function Library: /etc/rc.d/init.d/functions# Source Networking configuration: /etc/sysconfig/network# Check that networking are up. ["$NETWORKING" = "no"] && exit 0nginx= "/usr/sbin/nginx" prog=$ (basename $nginx) #NGINX_CONF_FILE = "/etc/nginx/ Nginx.conf "nginx_conf_file="/usr/local/nginx/conf/nginx.conf "[-f/etc/sysconfig/nginx] &&. /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs () {# Make required directories user= ' $nginx-v 2>&amp ; 1 | grep "Configure Arguments:.*--user=" | Sed ' s/[^*]*--user=\ ([^]*\). */\1/g '-' if [-N ' $user "]; Then if [-Z ' grep $user/etc/passwd ']; Then USEradd-m-s/bin/nologin $user fi options= ' $nginx-v 2>&1 | grep ' Configure arguments: ' for opt in $options; do if [' Echo $opt | grep '. *-temp-path ']; Then value= ' echo $opt | Cut-d "="-F 2 ' if [!-D "$value"];              Then # echo "Creating" $value mkdir-p $value && chown-r $user $value     fi fi Done Fi}start () {[-X $nginx] | | Exit 5 [-F $NGINX _conf_file] | | Exit 6 MAKE_DIRS    Echo-n $ "Starting $prog:" Daemon $nginx-C $NGINX _conf_file retval=$?  echo [$retval-eq 0] && touch $lockfile return $retval}stop () {echo-n $ "stopping $prog:" Killproc    $prog-quit retval=$?    echo [$retval-eq 0] && rm-f $lockfile return $retval}restart () {configtest | | return $?    Stop sleep 1 start}reload () {configtest | | return $? Echo-n $ "Reloading $prog:" Killproc $nginx-hup retval=$?   Echo}force_reload () {restart}configtest () {$nginx-T-C $NGINX _conf_file}rh_status () {Status $prog}rh_status_        Q () {rh_status >/dev/null 2>&1}case "$" in start) Rh_status_q && exit 0 $    ;; Stop) rh_status_q | |    Exit 0 $;;    Restart|configtest) \ n; Reload) Rh_status_q | |    Exit 7 $;;    Force-reload) force_reload;;    status) Rh_status;; Condrestart|try-restart) rh_status_q | |    Exit 0;;  *) echo $ "Usage: $ start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac# END

With the /root/service-nginx.sh replacement/etc/init.d/nginx

mv /root/service-nginx.sh /etc/init.d/nginx

Give executable limits

chmod 755 /etc/init.d/nginx

Perform

systemctl start nginx
Source mode installation Nginx, automated installation script
#!/bin/bash# installation configurationNGINX_VERSION=1.12.2NGINX_SRC_PATH=/rootNGINX_BIN_PATH=/usr/local/nginx# disable firewallsystemctl stop firewalldsetenforce 0# installation dependenceyum install -y pcre-devel zlib-devel openssl-devel wget gcc# download nginx source packagecd ${NGINX_SRC_PATH}wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz# unzip source packagetar -xzvf nginx-${NGINX_VERSION}.tar.gzcd ./nginx-${NGINX_VERSION}# install nginx./configure --prefix=${NGINX_BIN_PATH} --with-http_ssl_modulemake & make install# start nginx servicecd ${NGINX_BIN_PATH}/sbin./nginx# END

Linux under source installation and configuration Nginx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.