search
HomeOperation and MaintenanceNginxHow to install Nginx server program and simple environment configuration on Ubuntu

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:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment