Home Operation and Maintenance Nginx How to install and configure Nginx on Ubuntu

How to install and configure Nginx on Ubuntu

May 27, 2023 pm 10:52 PM
nginx 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
Copy after login

ubuntu Install nginx from ppa source:

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

ubuntu Install nginx from regular sources:

sudo apt-get install nginx
Copy after login

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
Copy after login

(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
Copy after login

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
Copy after login

(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 \
Copy after login
--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
Copy after login

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 \
Copy after login
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
Copy after login

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
Copy after login

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
Copy after login
5.5m /png/nginx/1.5.7/sbin/nginx
Copy after login

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"
Copy after login

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
Copy after login

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

mkdir /png/nginx/1.5.7/var/cache
Copy after login

Start nginx:

sudo /png/nginx/1.5.7/nginx start
Copy after login

Test page:

curl -i `hostname`
Copy after login

Look at the port:

sudo netstat -antp|grep nginx
Copy after login

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
Copy after login

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
Copy after login

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

cat /proc/25047/status|grep threads
Copy after login
threads: 1
Copy after login

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服务了,比如重载配置
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to start nginx in Linux How to start nginx in Linux Apr 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.

How to check whether nginx is started How to check whether nginx is started Apr 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 configure nginx in Windows How to configure nginx in Windows Apr 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 start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to solve nginx403 error How to solve nginx403 error Apr 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 solve nginx403 How to solve nginx403 Apr 14, 2025 am 10:33 AM

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

How to solve nginx304 error How to solve nginx304 error Apr 14, 2025 pm 12:45 PM

Answer to the question: 304 Not Modified error indicates that the browser has cached the latest resource version of the client request. Solution: 1. Clear the browser cache; 2. Disable the browser cache; 3. Configure Nginx to allow client cache; 4. Check file permissions; 5. Check file hash; 6. Disable CDN or reverse proxy cache; 7. Restart Nginx.

See all articles