search
HomeOperation and MaintenanceNginxHow to install and configure Nginx on Ubuntu

How to install and configure Nginx on Ubuntu

May 27, 2023 pm 10:52 PM
nginxubuntu

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 and configure Nginx on Ubuntu

You can also see similar content with top:

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

How to install and configure Nginx 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 and configure Nginx 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 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

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.