nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz
mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz php-7.4.29.tar.xz
//创建系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@localhost ~]# yum -y groups mark install 'Development Tools'
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
[root@localhost ~]# ll -d /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 9月 3 08:38 /var/log/nginx
//编译安装
[root@localhost ~]# tar xf nginx-1.22.0.tar.gz
[root@localhost ~]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.22.0]# make
[root@localhost nginx-1.22.0]# make install
//配置环境变量
[root@localhost ~]# ls /usr/local/nginx/
conf html logs sbin
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
//启动测试
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# nginx -s stop
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//关闭防火墙
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service nginx.service
[root@localhost system]# vim nginx.service
[root@localhost system]# cat nginx.service
[Unit]
Description=web server daemon
After=network.target
[Service]
Type=foring
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql
[root@localhost ~]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-5.7.37-linux-glibc2.12-x86_64 sbin src
[root@localhost local]# mv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll -d mysql
drwxr-xr-x 9 mysql mysql 129 9月 3 09:43 mysql
[root@localhost local]# cd mysql/
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ls
bin docs include lib LICENSE man README share support-files
[root@localhost mysql]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost mysql]# source /etc/profile.d/mysql.sh
[root@localhost mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost mysql]# ldconfig
[root@localhost mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@localhost mysql]# cd
[root@localhost ~]# mkdir -p /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data
[root@localhost ~]# ll -d /opt/data
drwxr-xr-x 2 mysql mysql 6 9月 3 09:58 /opt/data
[root@localhost ~]# mysqld --initialize --user mysql --datadir /opt/data
2022-09-03T13:59:03.305168Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-09-03T13:59:04.191459Z 1 [Note] A temporary password is generated for root@localhost: e-5MFq6MKIAR
[root@localhost ~]# echo 'e-5MFq6MKIAR' > pass
[root@localhost ~]# cat pass
e-5MFq6MKIAR
[root@localhost ~]# vim /etc/my.cnf
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@localhost ~]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]# vim mysqld
basedir=/usr/local/mysql
datadir=/opt/data
[root@localhost support-files]# chown -R mysql.mysql mysqld
[root@localhost support-files]# ll -d mysqld
-rwxr-xr-x 1 mysql mysql 10601 9月 3 10:03 mysqld
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# cp sshd.service mysqld.service
[root@localhost system]# vim mysqld.service
[root@localhost system]# cat mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]# systemctl daemon-reload
[root@localhost system]# cd
[root@localhost ~]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# cat pass
e-5MFq6MKIAR
[root@localhost ~]# mysql -uroot -p'e-5MFq6MKIAR'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
//编译安装
[root@localhost ~]# tar xf php-7.4.29.tar.xz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.22.0.tar.gz php-7.4.29.tar.xz
mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz pass
nginx-1.22.0 php-7.4.29
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@localhost ~]# cd php-7.4.29
[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@localhost php-7.4.29]# make
[root@localhost php-7.4.29]# make install
//配置环境变量
[root@localhost ~]# cd /usr/local/php7/
[root@localhost php7]# ls
bin etc include lib php sbin var
[root@localhost php7]# echo 'export PATH=/usr/local/php7/bin:/usr/local/php7/:sbin:$PATH' > /etc/profile.d/php7.sh
[root@localhost php7]# source /etc/profile.d/php7.sh
[root@localhost php7]# ln -s /usr/local/php7/include /usr/include/php
[root@localhost php7]# vim /etc/ld.so.conf.d/php.conf
[root@localhost php7]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php7/lib
[root@localhost php7]# ldconfig
[root@localhost php7]# cd
[root@localhost ~]# php -v
PHP 7.4.29 (cli) (built: Sep 3 2022 10:25:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
//配置php-fpm
[root@localhost ~]# cd php-7.4.29
[root@localhost php-7.4.29]# \cp php.ini-production /etc/php.ini
[root@localhost php-7.4.29]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.29]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.4.29]# ll /etc/rc.d/init.d/php-fpm
-rwxr-xr-x 1 root root 2402 9月 3 10:41 /etc/rc.d/init.d/php-fpm
[root@localhost ~]# cd /usr/local/php7/etc/
[root@localhost etc]# ls
pear.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# ls
pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
[root@localhost php-fpm.d]# ls
www.conf www.conf.default
[root@localhost php-fpm.d]# vim www.conf
listen = 127.0.0.1:9000
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
functions php-fpm README
[root@localhost init.d]# service php-fpm start
Starting php-fpm done
[root@localhost init.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost init.d]# service php-fpm stop
Gracefully shutting down php-fpm . done
[root@localhost init.d]# service php-fpm start
Starting php-fpm done
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# cat nginx.conf
#user nobody;
......
location / {
root html;
index index.php index.html index.htm; //添加index.php
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
//取消下面几行的注释
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
......
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim index.php
[root@localhost html]# cat index.php
[root@localhost html]# ls
50x.html index.html index.php
[root@localhost html]# mv index.html /opt/
[root@localhost html]# ls
50x.html index.php
[root@localhost html]# systemctl restart nginx
[root@localhost html]# cd
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 3月 23 2020 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rw-r--r--. 1 root root 474 3月 23 2020 /etc/rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
[root@localhost ~]# head /etc/rc.d/rc.local
#!/bin/bash
/usr/local/nginx/sbin/nginx
service mysqld start
service php-fpm start
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
访问页面为 “File not found.”
//解决方案
php-fpm找不到SCRIPT_FILENAME里执行的php文件
更改配置文件nginx.conf ,fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
改为
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
然后再重启nginx:进入nginx可执行目录sbin下,输入命令./nginx -s reload 即可
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[root@localhost ~]# nginx
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -s reload