How to Build and Deploy a Next.js App on Apache Server
<p><strong><span style="font-size: 16px">Step 1: Installing Next.js</span></strong></p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">npm install -g yarn
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span> -pv /var/www/<span style="color: rgba(0, 0, 0, 1)">project_folder_name
</span><span style="color: rgba(0, 0, 255, 1)">cd</span> /var/www/<span style="color: rgba(0, 0, 0, 1)">project_folder_name
yarn create next-app</span></pre>
</div>
<p>Edit package.json and replace the script section with the following:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
},</span></pre>
</div>
<p> </p>
<p><strong><span style="font-size: 16px">Step 2: Preparing Your Next.js Application for Production</span></strong></p>
<p>Navigate to your project's root directory and run the following command</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">cd</span> /var/www/<span style="color: rgba(0, 0, 0, 1)">project_folder_name
yarn install
yarn </span><span style="color: rgba(0, 0, 255, 1)">run</span> build</pre>
</div>
<p> </p>
<p><strong><span style="font-size: 16px">Step 3: Configuring Apache</span></strong></p>
<p>Edit the Apache configuration file, usually located at /etc/httpd/conf/httpd.conf. Add the following lines at the end of the file.</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">VirtualHost </span><span style="color: rgba(255, 0, 0, 1)">*:80</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)">
ServerName your-domain.com
ServerAdmin contact@example.com
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</span><span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">Directory </span><span style="color: rgba(255, 0, 0, 1)">"/var/www/project_folder_name"</span><span style="color: rgba(0, 0, 255, 1)">></span><span style="color: rgba(0, 0, 0, 1)">
AllowOverride All
</span><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">Directory</span><span style="color: rgba(0, 0, 255, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">VirtualHost</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
</div>
<p>Enable the necessary Apache mod_proxy and mod_proxy_http modules</p>
<div class="cnblogs_code">
<pre>LoadModule proxy_module modules/mod_proxy.<span style="color: rgba(0, 0, 0, 1)">so
LoadModule proxy_http_module modules</span>/mod_proxy_http.<span style="color: rgba(0, 0, 0, 1)">so
httpd -M</span></pre>
</div>
<p>Restart Apache to apply the changes.</p>
<div class="cnblogs_code">
<pre>sudo service httpd restart</pre>
</div>
<p> </p>
<p><strong><span style="font-size: 16px">Step 4: Deploying Your Next.js Application And Keeping Your Application Running</span></strong></p>
<p>Use yarn to start your Next.js application in production mode.</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">cd</span> /var/www/<span style="color: rgba(0, 0, 0, 1)">project_folder_name
yarn </span><span style="color: rgba(0, 0, 255, 1)">start</span></pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<br /><p><table><tr><td><img style="border-top-width: 0pt; border-left-width: 0pt; border-bottom-width: 0pt; border-right-width: 0pt" alt="Creative Commons License" src="http://i.creativecommons.org/l/by-nc/2.5/cn/88x31.png" /> </td><td>本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 </td></p><br><br>
来源:https://www.cnblogs.com/sekihin/p/18593760
頁:
[1]