详解CentOS 7.0源码包搭建LNMP 实际环境搭建
centos7+nginx1.11.7+mysql5.7.16+php7.1.0+openssl-1.1.0c
一、linux 系统限制配置
1、关闭系统防火墙
systemctl stop firewalld.service 关闭防火墙 systemctl disable firewalld.service 禁用防火墙
2、关闭selinux
sed -i 's/selinux=.*/selinux=disabled/g' /etc/selinux/config setenforce 0 selinux 立即生效
二、系统安装约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
三、下载软件包
1、下载nginx最新稳定版本
wget -p /usr/local/src http://nginx.org/download/nginx-1.11.7.tar.gz
2、下载mysql-boost-5.7.16 带 boost 如果不带源码安装如果网络环境不会可能会出现错误
wget -p /usr/local/src http://cdn.mysql.com/downloads/mysql-5.7/mysql-boost-5.7.16.tar.gz
3、下载php-7.1.0版本
wget -p /usr/local/src http://cn2.php.net/distributions/php-7.1.0.tar.gz
4、下载libmemcached-1.0.18
wget -p /usr/local/src https://launchpadlibrarian.net/165454254/libmemcached-1.0.18.tar.gz
5、下载php-memcached
yum -y install git cd /usr/local/src git clone -b php7 https://github.com/php-memcached-dev/php-memcached.git
6、下载openssl-1.1.0c
wget -p /usr/local/src https://www.openssl.org/source/openssl-1.1.0c.tar.gz
四、安装编译器及依赖
yum -y insyall epel-release yum -y install patch gcc gcc-c++ readline-devel zlib-devel libffi-devel \ openssl openssl-devel make autoconf automake libtool bison libxml2 \ libxml2-devel libxslt-devel libyaml-devel python python-docutils \ cmake imake expat-devel libaio libaio-devel bzr ncurses-devel wget \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \ pcre-devel curl-devel libmcrypt libmcrypt-devel
五、编译安装mysql-boost-5.7.16 方便再次安装创建mysql_install.sh脚本
1、mysql_install.sh内容
#!/bin/bash #yum update -y #yum install -y cmake gcc-c++ ncurses-devel gcc make openssl* #mysql安装脚本 dbdir='/data/mysql' #mysql数据存储目录 mysqldir='/usr/local/mysql' # mysql安装目录 passwd='123456' # mysql root密码 安装完成可远程ip登陆 [ -d $dbdir ] || mkdir $dbdir -p id mysql &> /dev/null if [ $? -ne 0 ];then useradd mysql -s /sbin/nologin -m fi chown -r mysql:mysql $dbdir cd /usr/local/src tar -xvf mysql-boost-5.7.16.tar.gz cd mysql-5.7.16 cmake . -dcmake_install_prefix=$mysqldir \ -dmysql_datadir=$dbdir \ -dsysconfdir=/etc \ -dwith_innobase_storage_engine=1 \ -dwith_archive_storage_engine=1 \ -dwith_blackhole_storage_engine=1 \ -dwith_readline=1 \ -dwith_libwrap=0 \ -dmysql_unix_addr=/tmp/mysql.sock \ -dwith_ssl=system \ -dwith_zlib=system \ -dwith_boost=/usr/local/src/mysql-5.7.16/boost/boost_1_59_0 \ -ddefault_charset=utf8 \ -ddefault_collation=utf8_general_ci if [ $? != 0 ];then echo "cmake error!" exit 1 fi make && make install if [ $? -ne 0 ];then echo "install mysql is failed!" && /bin/false fi sleep 2 chown -r mysql:mysql $mysqldir chown -r root:root $mysqldir cp $mysqldir/support-files/my-default.cnf /etc/my.cnf echo export path=$path:$mysqldir/bin:$mysqldir/lib >>/etc/profile source /etc/profile cat >> /etc/my.cnf << eof character_set_server = utf8 basedir = $mysqldir datadir = $dbdir port = 3306 server_id = 1 socket = /tmp/mysql.sock explicit_defaults_for_timestamp=true eof sed -i 's/sql_mode=.*/sql_mode=no_engine_substitution,strict_trans_tables,no_auto_create_user/g' /etc/my.cnf source /etc/profile sleep 5 cd $mysqldir cp support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld mysql_ssl_rsa_setup rm -rf $dbdir mysqld --initialize --user=mysql if [ $? -ne 0 ];then echo "install mysql is failed!" && /bin/false fi #/etc/init.d/mysqld stop mysqld_safe --user=mysql --skip-grant-tables --skip-networking & sleep 5 echo "update user set authentication_string=password('$passwd') where user='root'; flush privileges;" | mysql mysql echo "set password=password('$passwd'); flush privileges;" | mysql -u root -p$passwd --connect-expired-password sleep 5 echo "grant all privileges on *.* to root@'%' identified by '$passwd'; flush privileges; " | mysql -u root -p$passwd /etc/init.d/mysqld restart if [ $? -ne 0 ];then echo "install mysql is failed!" && /bin/false fi idso=`cat /etc/ld.so.conf| grep $mysqldir/lib | wc -l ` if [ $idso -eq 0 ];then echo "$mysqldir/lib" >> /etc/ld.so.conf ldconfig fi chkconfig mysqld on
2、给 mysql_install.sh 可执行权限
chmod +x mysql_install.sh
3、运行mysql_install.sh
./mysql_install.sh
六、编译安装php7 创建php安装脚本php7_install.sh
1、vim php7_install.sh
#!/bin/bash if [ $( find / -name mysql | wc -l ) -gt 1 ];then echo " mysql is install " else yum install -y mysql fi cd /usr/local/src tar -xzvf php-7.1.0.tar.gz cd ./php-7.1.0 ./configure \ --prefix=/usr/local/php7 \ --exec-prefix=/usr/local/php7 \ --with-config-file-path=/usr/local/php7/etc \ --with-curl \ --with-freetype-dir \ --with-gd \ --with-gettext \ --with-iconv-dir \ --with-kerberos \ --with-libdir=lib64 \ --with-libxml-dir \ --with-mysqli \ --with-openssl \ --with-pcre-regex \ --with-pdo-mysql \ --with-pdo-sqlite \ --with-pear \ --with-png-dir \ --with-xmlrpc \ --with-xsl \ --with-zlib \ --with-zlib-dir \ --with-mhash \ --with-mcrypt \ --with-openssl-dir \ --with-jpeg-dir \ --enable-fpm \ --enable-bcmath \ --enable-libxml \ --enable-inline-optimization \ --enable-gd-native-ttf \ --enable-mbregex \ --enable-mbstring \ --enable-opcache \ --enable-pcntl \ --enable-shmop \ --enable-soap \ --enable-sockets \ --enable-sysvsem \ --enable-xml \ --enable-zip make && make install # 中文php画图取消这个参数,不然会出现乱码 # --enable-gd-jis-conv \
2、给 php7_install.sh 可执行权限
chmod +x php7_install.sh
3、执行 php7_install.sh
./php7_install.sh
4、编译安装libmemcached-1.0.18
vim libmemcached_install.sh
#/!bin/bash cd /usr/local/src tar -zxvf libmemcached-1.0.18.tar.gz cd ./libmemcached-1.0.18 ./configure --prefix=/usr/local/libmemcached make && make install chmod +x libmemcached_install.sh ./libmemcached_install.sh
5、编译安装php-memcached
vim memcached_install.sh
#!/bin/bash cd /usr/local/src/php-memcached /usr/local/php7/bin/phpize ./configure --with-libmemcached-dir=/usr/local/libmemcached \ --with-php-config=/usr/local/php7/bin/php-config \ --disable-memcached-sasl make && make install chmod +x memcached_install.sh ./memcached_install.sh
留意编完成生成文件路径
installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
七、编译安装openssl-1.1.0c
vim openssl_install.sh
#!/bin/bash #openssl install cd /usr/local/src tar -xvf openssl-1.1.0c.tar.gz cd /usr/local/src/openssl-1.1.0c ./config --openssldir=/usr/local/ssl make && make install ./config shared --openssldir=/usr/local/ssl make clean make && make install idso=`cat /etc/ld.so.conf| grep /usr/local/lib64 | wc -l ` if [ $idso -eq 0 ];then echo "/usr/local/lib64" >> /etc/ld.so.conf fi ldconfig chmod +x openssl_install.sh ./openssl_install.sh
八、编译安装nginx-1.11.7
vim nginx_install.sh
#!/bin/bash # nginx install id nginx &> /dev/null if [ $? -ne 0 ];then groupadd -r nginx useradd -g nginx -r nginx fi cd /usr/local/src tar -xvf nginx-1.11.7.tar.gz cd /usr/local/src/nginx-1.11.7 ./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --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-http_auth_request_module \ --with-threads \ --with-stream \ --with-openssl=/usr/local/src/openssl-1.1.0c \ # openssl 源码解压路径 --with-stream_ssl_module \ --with-http_slice_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-http_v2_module \ --with-ipv6 mkdir -pv /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp} make && make install
2、给nginx_install.sh可执行权限
chmod +x nginx_install.sh ./nginx_install.sh
九、配置php7
/usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/memcached.so
这个路径是 随机可变的所以要注意
留意变完成生成文件路径
installing shared extensions: /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
cd /usr/local/src/php-7.1.0 cp php.ini-production /usr/local/php7/etc/php.ini cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf sed -i "s/user = .*/user = nginx/g" /usr/local/php7/etc/php-fpm.d/www.conf sed -i "s/group = .*/group = nginx/g" /usr/local/php7/etc/php-fpm.d/www.conf cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm chkconfig php-fpm on cat >> /usr/local/php7/etc/php.ini<< eof soap.wsdl_cache_enabled=1 max_input_time = 600 max_execution_time = 300 date.timezone = asia/shanghai post_max_size = 32m memory_limit = 128m mbstring.func_overload = 1 extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/memcached.so eof cat > /usr/local/nginx/html/index.php<<eof <?php phpinfo(); ?> eof service php-fpm start
十、配置nginx
1、重命名:/etc/nginx/nginx.conf
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.back
2、新建/etc/nginx/nginx.conf
cat > /etc/nginx/nginx.conf << eof user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ' '\$status \$body_bytes_sent "\$http_referer" ' '"\$http_user_agent" "\$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } eof
3、创建/etc/nginx/conf.d
mkdir -p /etc/nginx/conf.d
4、创建支持php-fpm web nginx配置
cat > /etc/nginx/conf.d/default.conf << eof server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/local/nginx/html; index index.php index.html index.htm; } #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 /usr/local/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } location ~* ^.+\.(jpg|jpeg|gif|png|bmp)$ { access_log off; root opencart; expires 30d; break; } } eof
5、创建nginx启动脚本
vim /etc/init.d/nginx
# chkconfig: 2345 10 90 # description: start and stop nginx path=/usr/local/bin:/sbin:/usr/bin:/bin exec=/usr/sbin/nginx pidfile=/var/run/nginx.pid conf="/etc/nginx/nginx.conf" auth="1234" case "$1" in start) if [ -f $pidfile ] then echo "$pidfile exists, process is already running or crashed." else echo "starting nginx server..." $exec -c $conf & fi if [ "$?"="0" ] then echo "nginx is running..." fi ;; stop) if [ ! -f $pidfile ] then echo "$pidfile exists, process is not running." else pid=$(cat $pidfile) echo "stopping..." kill -9 $pid pid=$(pidof nginx) kill -9 $pid rm -rf /var/run/nginx.pid sleep 2 while [ -x $pidfile ] do echo "waiting for nginx to shutdown..." sleep 1 done echo "nginx stopped" fi ;; reload) $exec -s reload ;; restart|force-reload) ${0} stop ${0} start ;; *) echo "usage: /etc/init.d/nginx {start|stop|restart|force-reload|reload}" >&2 exit 1 esac
6、给 /etc/init.d/nginx 可执行权限
chmod +x /etc/init.d/nginx
7、设置开机启动
chkconfig nginx on
8、启动nginx
service nginx start
十一、测试
[root@qka169 src]# openssl version openssl 1.1.0c 10 nov 2016 mysql -u root -p123456 mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 看看是否登陆成功。远程带ip是否登陆成功 mysql -u root -h192.168.1.69 -p123456 mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) 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 6 server version: 5.7.16 source distribution copyright (c) 2000, 2016, oracle and/or its affiliates. all rights reserved. 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>
测试nginx 是否能打开
[root@qka169 html]# ps -ef | grep php-fpm root 337433 1 0 18:03 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf) nobody 337434 337433 0 18:03 ? 00:00:00 php-fpm: pool www nobody 337435 337433 0 18:03 ? 00:00:00 php-fpm: pool www root 337454 37888 0 18:12 pts/0 00:00:00 grep --color=auto php-fpm [root@qka169 html]# ps -ef | grep nginx root 337400 1 0 18:01 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 337401 337400 0 18:01 ? 00:00:00 nginx: worker process root 337456 37888 0 18:13 pts/0 00:00:00 grep --color=auto nginx [root@qka169 html]# netstat -nalp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* listen 337400/nginx: maste tcp 0 0 192.168.1.69:80 192.168.6.6:54714 time_wait - tcp 0 0 192.168.1.69:80 192.168.6.6:54709 time_wait - 远程打开 http://192.168.1.69/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。