Home >Operation and Maintenance >Nginx >How to install Nginx server program and simple environment configuration on Ubuntu

How to install Nginx server program and simple environment configuration on Ubuntu

王林
王林forward
2023-05-12 12:31:061356browse

ubuntu Install nginx from official source

cd ~ 
wget http://nginx.org/keys/nginx_signing.key 
sudo apt-key add nginx_signing.key 
sudo nano /etc/apt/sources.list   # 添加以下两句 
deb http://nginx.org/packages/ubuntu/ precise nginx 
deb-src http://nginx.org/packages/ubuntu/ precise nginx 
sudo apt-get update 
sudo apt-get install nginx

ubuntu Install nginx from ppa source:

sudo add-apt-repository ppa:nginx/stable 
sudo apt-get update 
sudo apt-get install nginx

ubuntu Install nginx from regular sources:

sudo apt-get install nginx

Compile and install nginx

wget http://nginx.org/packages/mainline/ubuntu/pool/nginx/n/nginx/nginx_1.5.7-1~precise_i386.deb 
wget http://nginx.org/download/nginx-1.5.7.tar.gz
tar xzf nginx-1.5.7.tar.gz
cd nginx-1.5.7

(Note: nginx1.5.7 is the mainline version and Non-stable version)

In order to facilitate development and management, I created a new png directory in the root directory, and set the directory owner to the current user, and nginx was compiled under /png/nginx/1.5.7:

sudo mkdir /png
sudo chown eechen:eechen /png

I defined the running user as png:png, so I need to create a new user like this:

sudo addgroup png --system
sudo adduser png --system --disabled-login --ingroup png --no-create-home --home /nonexistent --gecos "png user" --shell /bin/false

(For the command to create a new user, please refer to the pre-installed command in the official deb package. Installation script debian/preinst)

The compilation parameters refer to the deb package officially provided by nginx (visible by nginx -v).

./configure \
--prefix=/png/nginx/1.5.7 \
--sbin-path=/png/nginx/1.5.7/sbin/nginx \
--conf-path=/png/nginx/1.5.7/conf/nginx.conf \
--error-log-path=/png/nginx/1.5.7/var/log/error.log \
--http-log-path=/png/nginx/1.5.7/var/log/access.log \
--pid-path=/png/nginx/1.5.7/var/run/nginx.pid \
--lock-path=/png/nginx/1.5.7/var/run/nginx.lock \
--http-client-body-temp-path=/png/nginx/1.5.7/var/cache/client_temp \
--http-proxy-temp-path=/png/nginx/1.5.7/var/cache/proxy_temp \
--http-fastcgi-temp-path=/png/nginx/1.5.7/var/cache/fastcgi_temp \
--http-uwsgi-temp-path=/png/nginx/1.5.7/var/cache/uwsgi_temp \
--http-scgi-temp-path=/png/nginx/1.5.7/var/cache/scgi_temp \
--user=png \
--group=png \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6

Note: This step installs dependent packages according to the error prompts , this is when apt is about to show its power. For example, my system has these packages installed:

sudo apt-get -y install \
build-essential \
autoconf \
libtool \
libxml2 \
libxml2-dev \
openssl \
libcurl4-openssl-dev \
libbz2-1.0 \
libbz2-dev \
libjpeg-dev \
libpng12-dev \
libfreetype6 \
libfreetype6-dev \
libldap-2.4-2 \
libldap2-dev \
libmcrypt4 \
libmcrypt-dev \
libmysqlclient-dev \
libxslt1.1 \
libxslt1-dev \
libxt-dev \
libpcre3-dev

After installing these packages, you don’t need to install them again next time you compile a new version of nginx, and it’s basically the same. Meet the configure requirements when compiling php.
Okay, after the configure is successful, you can compile and install:

time make && make install

time is mainly used to check the time taken for this compilation.
After compilation You can take a look at the size of this guy:

du -sh /png/nginx/1.5.7/sbin/nginx
5.5m /png/nginx/1.5.7/sbin/nginx

Simple environment configuration summary
Reduce the file size after nginx is compiled:
Edit the source file nginx-1.5. 7/auto/cc/gcc Remove debug information (just comment it out):

# debug 
# cflags="$cflags -g"

The size of the compiled main program is more than 700k, which is similar to the size of the deb package program officially provided by nginx .
In addition, if you remove some unnecessary modules when configuring, the compiled executable file will be smaller.
Of course, I need a service script to manage nginx. At this time, I can also use the official deb package provided Service script etc/init.d/nginx.
I put it in /png/nginx/1.5.7/nginx and slightly modified the values ​​defined at the beginning (lines 13 to 19):

path=/sbin:/usr/sbin:/bin:/usr/bin
desc=nginx
name=nginx
conffile=/etc/nginx/nginx.conf
daemon=/usr/sbin/nginx
pidfile=/var/run/$name.pid
scriptname=/etc/init.d/$name
改为
path=/sbin:/usr/sbin:/bin:/usr/bin
desc=nginx
name=nginx
conffile=/png/nginx/1.5.7/conf/nginx.conf
daemon=/png/nginx/1.5.7/sbin/nginx
pidfile=/png/nginx/1.5.7/var/run/$name.pid
scriptname=/png/nginx/1.5.7/$name

Create a cache directory before starting, otherwise an error will be prompted:

mkdir /png/nginx/1.5.7/var/cache

Start nginx:

sudo /png/nginx/1.5.7/nginx start

Test page:

curl -i `hostname`

Look at the port:

sudo netstat -antp|grep nginx

Check the memory it occupies:
htop press f4 to filter nginx

How to install Nginx server program and simple environment configuration on Ubuntu

You can also see similar content with top:

top -b -n1|head -n7 && top -b -n1|grep nginx

How to install Nginx server program and simple environment configuration on Ubuntu

Mainly depends on the value of res, resident memory (resident), excluding the physics of swap space Memory, the unit is kb, %mem takes res as the reference object.
You can see that the total physical memory occupied by the two nginx processes is less than 2m, and the memory usage is very small.
In addition, the res value in top Corresponding to the value of rss in ps aux:

ps aux|head -n1 && ps aux|grep nginx

Also we can see that the nginx worker process has only one thread:

cat /proc/25047/status|grep threads
threads: 1

where 25047 is the nginx worker process pid No.
Make nginx a system service and start it automatically at boot:

sudo ln -s /png/nginx/1.5.7/nginx /etc/init.d/png-nginx
sudo update-rc.d png-nginx defaults #开机自启动 
sudo update-rc.d -f png-nginx remove # 以后不想开机自启动可以这样禁止
sudo service png-nginx reload #这样就可以用service来管理nginx服务了,比如重载配置

Finally, the main configuration file of nginx is located at /png/nginx/1.5.7/conf/nginx.conf. Configure on demand.

The above is the detailed content of How to install Nginx server program and simple environment configuration on Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete