Centos7之LNMP环境编译安装
一、系统环境准备
注:安装时间过长,只做参考!!!
1、系统信息
[root@localhost ~]# uname -r
3.10.0-957.el7.x86_64
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
2、关闭防火墙
vim /etc/selinux/config 将SELINUX的值改为如下:
SELINUX=disabled
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
注:此处是测试环境,在生产环境中,按需开启对应应用端口,不能直接关闭防火墙!!
3、配置地址信息
[root@localhost src]# cat /etc/sysconfig/network-scripts/ifcfg-ens32 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no NAME=ens32 DEVICE=ens32 ONBOOT=yes IPADDR=172.16.8.5 PREFIXO=24 GATEWAY=172.16.8.1 DNS1=202.96.134.133 DNS2=119.29.29.29
4、更改主机名称
[root@localhost ~]# vi /etc/hostname #将默认的 localhost.localdomain 更改为需要的主机名称
5、更新yum源
a、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
b、下载新的CentOS-Base.repo 到/etc/yum.repos.d/
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
c、之后运行yum makecache生成缓存
注:可直接使用官方的yum源!!!
6、重启系统
[root@localhost ~]# init 6
或者
[root@localhost ~]# reboot now
二、准备工具、软件包及编译环境
1、安装下载工具
yum install wget -y
2、安装文件传输工具
yum install -y lrzsz
3、安装编译环境
yum install -y gcc gcc-c++
4、安装解压软件
yum install bzip2 -y
5、下载软件包
[root@LNMP ~]# cd /usr/local/src/
[root@LNMP src]# wget -c http://nginx.org/download/nginx-1.16.0.tar.gz
[root@LNMP src]# wget -c http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.16.tar.gz
[root@LNMP src]# wget -c https://www.php.net/distributions/php-7.3.5.tar.bz2
6、解压软件包
[root@LNMP src]# for i in *.tar.gz;do tar xvf $i;done && tar -jxvf *.bz2
三、编译安装Nginx
1、创建运行账户及组
[root@LNMP ~]# groupadd -r nginx && useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx -M nginx
2、进入解压目录
[root@LNMP ~]# cd /usr/local/src/nginx-1.16.0
[root@LNMP nginx-1.16.0]# pwd
/usr/local/src/nginx-1.16.0
3、安装依赖包
[root@LNMP nginx-1.16.0]# yum install -y pcre-devel openssl openssl-devel
4、编译安装
[root@LNMP nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_sub_module --with-stream --with-stream=dynamic [root@LNMP nginx-1.16.0]# make -j `grep 'processor' /proc/cpuinfo |wc -l` && make install
5、查看版本
[root@LNMP nginx-1.16.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.16.0
6、修改配置文件
#备份配置文件: [root@LNMP nginx-1.16.0]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back
#将 /usr/local/nginx/conf/nginx.conf替换为以下内容: ----------------------------------------------------------- user nginx nginx; #定义启动nginx的用户 worker_processes 2; #定义子进程数量,设置值和CPU核心数一致 error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; #nginx最多可打开的文件数量 events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server_tokens off; #配置隐藏版本号 include vhost/*.conf; #配置支持虚拟主机 } -----------------------------------------------------------
7、创建虚拟主机配置
#创建虚拟主机文件夹: [root@localhost nginx-1.16.0]# cd /usr/local/nginx/conf/ [root@LNMP conf]# mkdir vhost [root@LNMP conf]# pwd /usr/local/nginx/conf
#创建serve主机配置文件(此配置文件也可添加在nginx的配置文件中): [root@LNMP conf]# vi /usr/local/nginx/conf/vhost/default.conf ----------------------------------------------------------- server { listen 80; #监听端口 server_name localhost; #域名 index index.html index.htm index.php; #index.php配置解析php root /usr/local/nginx/html; #站点目录 location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } -----------------------------------------------------------
8、配置启动脚本
#创建启动脚本 [root@LNMP ~]# vi /etc/init.d/nginx ----------------------------------------------------------- #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL -----------------------------------------------------------
#配置脚本权限及开机启动 [root@LNMP ~]# chmod 755 /etc/init.d/nginx [root@LNMP ~]# chkconfig --add nginx [root@LNMP ~]# chkconfig nginx on
9、检查配置文件正确性
[root@LNMP ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
10、启动nginx
[root@LNMP ~]# /usr/local/nginx/sbin/nginx [root@LNMP ~]# ps aux|grep nginx root 27232 0.0 0.0 45964 1156 ? Ss 08:52 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 27233 0.0 0.2 48500 4040 ? S 08:52 0:00 nginx: worker process nginx 27234 0.0 0.2 48500 4284 ? S 08:52 0:00 nginx: worker process root 27236 0.0 0.0 112712 964 pts/0 S+ 08:54 0:00 grep --color=auto nginx
11、访问站点
12、Nginx其他命令
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件 /usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
四、编译安装Mysql
注:由于8.x版本的Mysql编译安装对编译器版本有要求,另不再支持make,而需要使用cmake编译。
1、创建运行账户及组
[root@LNMP mysql-8.0.16]# useradd -r -U mysql -M -d /usr/local/mysql/data -U :创建一组具有相同名称的用户并将用户添加到这个组 -r:创建系统用户 -M:不创建用户的家目录 -d:指定用户的始启动目录
2、安装scl源
[root@LNMP mysql-8.0.16]# yum install centos-release-scl scl-utils-build -y #编译安装需高版本gcc gcc-c++(高于5.3版本)
3、安装gcc
[root@LNMP mysql-8.0.16]# yum install devtoolset-8-gcc.x86_64 devtoolset-8-gcc-c++.x86_64 devtoolset-8-gcc-gdb-plugin.x86_64 -y [root@LNMP mysql-8.0.16]# source /opt/rh/devtoolset-8/enable #将此版本信息写入环境变量中(当前用户生效)
4、安装cmake3
#下载安装包 [root@LNMP src]# wget -c https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz
#解压编译安装 [root@LNMP src]# tar xvf cmake-3.6.2.tar.gz [root@LNMP src]# cd cmake-3.6.2 [root@LNMP cmake-3.6.2]# ./bootstrap
#默认安装在/usr/local [root@LNMP cmake-3.6.2]# make [root@LNMP cmake-3.6.2]# make install
5、安装依赖包
[root@LNMP mysql-8.0.16]# yum install ncurses-devel openssl-devel git -y
6、编译安装
[root@LNMP mysql-8.0.16]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/mysql/etc -DWITH_DEBUG=0 -DWITH_INNODB_MEMCACHED=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DENABLED_PROFILING=0 -DFORCE_INSOURCE_BUILD=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql -DWITH_SSL=system -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 注释: -DCMAKE_INSTALL_PREFIX=/usr/local/mysql #安装目录 -DMYSQL_DATADIR=/usr/local/mysql/data #数据目录 -DSYSCONFDIR=/usr/local/mysql/etc #系统文件目录 -DWITH_DEBUG=0 #是否开启debug调试支持,默认禁用(禁用0 开启1) -DWITH_INNODB_MEMCACHED=1 #是否生成memcached共享库,默认禁用 -DWITH_PARTITION_STORAGE_ENGINE=1 #是否将存储引擎PARTITION静态编译到服务器中 -DENABLED_PROFILING=0 #是否启用查询分析代码,默认ON -DFORCE_INSOURCE_BUILD=1 #是否强制执行源内构建,默认OFF -DDOWNLOAD_BOOST=1 #是否要下载Boost库,默认OFF -DWITH_BOOST=/usr/local/mysql #Boost库源的位置 -DWITH_SSL=system #ssl支持类型,默认是系统自带 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock [root@LNMP mysql-8.0.16]# make -j `grep 'processor' /proc/cpuinfo |wc -l` && make install
7、初始化
[root@LNMP mysql-8.0.16]#cd /usr/local/mysql [root@LNMP mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 注:初始化会生产mysql登录的默认密码!! 2019-11-21T03:14:15.514248Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2019-11-21T03:14:15.515026Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) initializing of server in progress as process 13963 2019-11-21T03:14:21.930205Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: %wlwtEaGa8_a 2019-11-21T03:14:24.402763Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.16) initializing of server has completed
8、配置启动脚本
#拷贝启动脚本文件 [root@LNMP mysql-8.0.16]# cp ./support-files/mysql.server /etc/init.d/mysqld
#修改配置 [root@LNMP mysql-8.0.16]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/usr/local/mysql/data
#授权 [root@LNMP mysql-8.0.16]# chmod 755 /etc/init.d/mysqld
#添加设置开机启动项 [root@LNMP mysql-8.0.16]# chkconfig --add mysqld [root@LNMP mysql-8.0.16]# chkconfig mysqld on
9、创建日志存放文件
#创建日志文件 [root@LNMP mysql-8.0.16]# cd /usr/local/mysql/ [root@LNMP mysql]# mkdir log [root@LNMP mysql]# touch ./log/mysql_error.log
#设置权限 [root@LNMP local]# chown -R mysql:mysql /usr/local/mysql/
10、修改配置文件
[root@LNMP mysql]# vim /etc/my.cnf #修改路径 [mysqld] datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock [mysqld_safe] log-error=/usr/local/mysql/log/mysql_error.log pid-file=/usr/local/mysql/log/mysql.pid
11、配置环境变量
[root@LNMP mysql]# cp /usr/local/mysql/bin/mysql /usr/bin/
12、启动
[root@LNMP log]# service mysqld start Starting MySQL...... SUCCESS!
13、更改root密码
[root@LNMP ~]# mysql -uroot -p Enter password: #连接登录mysql,这里的密码是mysql初始化时生成的哪个密码。
#更改密码为1qaz2wsx mysql> alter user root@'localhost' identified by '1qaz2wsx'; Query OK, 0 rows affected (0.07 sec) #刷新权限 mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)
五、编译安装PHP
1、创建Nginx运行账户及组
[root@LNMP php-7.3.5]# groupadd -r php7 && useradd -r -g php7 -s /sbin/nologin -d /usr/local/nginx -M php7
2、安装依赖
[root@LNMP php-7.3.5]# yum install libxml2-devel libxml2 libcurl-devel libpng libpng-devel libjpeg libjpeg-devel freetype-devel -y
3、编译安装
[root@LNMP php-7.3.5]# ./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --enable-fpm --with-fpm-user=php7 --with-fpm-group=php7 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/usr/local/mysql/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --enable-soap --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl --enable-bcmath --enable-sockets --with-gettext [root@LNMP php-7.3.5]# make -j `grep 'processor' /proc/cpuinfo |wc -l` && make install
4、拷贝php配置文件
[root@LNMP php-7.3.5]# cp php.ini-production /usr/local/php7/etc/php.ini
5、拷贝php-fpm服务并授执行权限
[root@LNMP php-7.3.5]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@LNMP php-7.3.5]# chmod +x /etc/init.d/php-fpm [root@LNMP php-7.3.5]# chkconfig --add php-fpm [root@LNMP php-7.3.5]# chkconfig php-fpm on
6、配置php-fpm进程配置文件和扩展配置文件
#默认拷贝对应文件即可,有需求可安装要求更改配置文件 [root@LNMP php-7.3.5]# cp sapi/fpm/php-fpm.conf /usr/local/php7/etc/php-fpm.conf [root@LNMP php-7.3.5]# cp sapi/fpm/www.conf /usr/local/php7/etc/php-fpm.d/www.conf
#以下为简单需求配置文件更改 ----------------------------------------------------------- #php-fpm进程配置 [root@LNMP php-7.3.5]# cp sapi/fpm/php-fpm.conf /usr/local/php7/etc/php-fpm.conf #修改配置文件
[root@LNMP etc]# vim php-fpm.conf #模块[global]定义pid及log文件路径,去掉注释; [global] pid = /usr/local/php7/var/run/php-fpm.pid error_log = /usr/local/php7/var/log/php-fpm.log #扩展文件配置
[root@LNMP php-7.3.5]# cp sapi/fpm/www.conf /usr/local/php7/etc/php-fpm.d/www.conf
#修改[www]池相关配置参数,可用默认 user = php7 group = php7 listen = 127.0.0.1:9000 # 或者指定php-fcgi.sock文件路径 listen.mode = 0660 pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 -----------------------------------------------------------
7、配置环境变量
[root@LNMP php-7.3.5]# vim /etc/profile #文末添加 PATH=$PATH:/usr/local/php7/bin export PATH #生效 [root@LNMP php-7.3.5]# source /etc/profile
8、查看php版本
[root@LNMP php-7.3.5]# php -v PHP 7.3.5 (cli) (built: Nov 24 2019 09:04:14) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
9、启动php-fpm
[root@LNMP php-fpm.d]# /etc/init.d/php-fpm start Starting php-fpm done [root@LNMP php-fpm.d]# ps -aux|grep php root 11375 0.0 0.3 219928 6472 ? Ss 10:39 0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) php7 11376 0.0 0.2 219928 5928 ? S 10:39 0:00 php-fpm: pool www php7 11377 0.0 0.2 219928 5928 ? S 10:39 0:00 php-fpm: pool www php7 11378 0.0 0.2 219928 5928 ? S 10:39 0:00 php-fpm: pool www php7 11379 0.0 0.2 219928 5928 ? S 10:39 0:00 php-fpm: pool www php7 11380 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11381 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11382 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11383 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11384 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11385 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www php7 11386 0.0 0.2 219928 5932 ? S 10:39 0:00 php-fpm: pool www
六、Nginx配置支持PHP
[root@LNMP php-fpm.d]#cd /usr/local/nginx/conf/vhost [root@LNMP vhost]# vim default.conf #配置如下 server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } }
七、编译php探测文件
[root@LNMP vhost]# cd /usr/local/nginx/html/ [root@LNMP html]# vim index.php #内容如下 <?php phpinfo(); ?>
八、验证
#重启PHP [root@LNMP html]# service php-fpm stop Gracefully shutting down php-fpm . done [root@LNMP html]# service php-fpm restart Gracefully shutting down php-fpm warning, no pid file found - php-fpm is not running ? Starting php-fpm done [root@LNMP html]# ps -ef|grep php root 11496 1 0 11:07 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) php7 11497 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11498 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11499 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11500 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11501 11496 0 11:07 ? 00:00:00 php-fpm: pool www #重载nginx [root@LNMP html]# /usr/local/nginx/sbin/nginx -s reload
#重启PHP [root@LNMP html]# service php-fpm stop Gracefully shutting down php-fpm . done [root@LNMP html]# service php-fpm restart Gracefully shutting down php-fpm warning, no pid file found - php-fpm is not running ? Starting php-fpm done [root@LNMP html]# ps -ef|grep php root 11496 1 0 11:07 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) php7 11497 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11498 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11499 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11500 11496 0 11:07 ? 00:00:00 php-fpm: pool www php7 11501 11496 0 11:07 ? 00:00:00 php-fpm: pool www #重载nginx [root@LNMP html]# /usr/local/nginx/sbin/nginx -s reload